看代码写结果
写出下列代码运行结果:
def f(x):
return x * x
if name == ' main ':
r = map(f, [1, 2, 3, 4, 5, 6, 7, 8, 9])
s=map(str, [1, 2, 3, 4, 5, 6, 7, 8, 9])
print(type(r))
print(type(s))
print(list(s))
print(list(r))
<class ‘map’>
<class ‘map’>
[‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’]
[1, 4, 9, 16, 25, 36, 49, 64, 81]
写出下列代码运行结果:
from functools import reduce def add(x, y):
return x + y
if name == ' main ':
result=reduce(add, [1, 3, 5, 7, 9])
print(type(result))
print(result)
<class ‘int’>
25
写出下列代码运行结果;
from functools import reduce
def fn(x, y):
return x * 10 + y
def CharToInt(s):
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
if name == ' main ':
result=reduce(fn, map(CharToInt, '13579'))
print(result)
13579
写出下列代码运行结果:
from functools import reduce
def CharToInt(s):
return {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}[s]
if name == ' main ':
result=reduce(lambda x, y: x * 10 + y, map(CharToInt, '13579'))
print(result)
13579
写出下列代码运行结果:
def is_odd(n):
return n % 2 == 1
def not_empty(s):
return s and s.strip()
if name == ' main ':
result=filter(is_odd, [1, 2, 4, 5, 6, 9, 10, 15])
print(type(result))
print(list(result))
print(list(filter(not_empty, ['A', '', 'B', None, 'C', ' '])))
<class ‘filter’>
[1, 5, 9, 15]
[‘A’, ‘B’, ‘C’]
看代码写结果;
from operator import itemgetter
L = ['bob', 'about', 'Zoo', 'Credit'] print(sorted(L))
print(sorted(L, key=str.lower))
students = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
print(sorted(students, key=itemgetter(0)))
print(sorted(students, key=lambda t: t[1]))
print(sorted(students, key=lambda t: t[1],reverse=True))
print(sorted(students, key=itemgetter(1), reverse=True))
[‘Credit’, ‘Zoo’, ‘about’, ‘bob’]
[‘about’, ‘bob’, ‘Credit’, ‘Zoo’]
[(‘Adam’, 92), (‘Bart’, 66), (‘Bob’, 75), (‘Lisa’, 88)]
[(‘Bart’, 66), (‘Bob’, 75), (‘Lisa’, 88), (‘Adam’, 92)]
[(‘Adam’, 92), (‘Lisa’, 88), (‘Bob’, 75), (‘Bart’, 66)]
[(‘Adam’, 92), (‘Lisa’, 88), (‘Bob’, 75), (‘Bart’, 66)]
写代码
写出下列运行代码,并用lambda表达式改写:
def square(num):
return num**2
items = [1, 2, 3, 4]
squared = list(map(square, items))
print(squared)
改写后:
结果是:[1, 4, 9, 16]
写出下列代码运行结果:
from collections.abc import Iterable, Iterator
def g():
yield 1
yield 2
yield 3
print('Iterable? [1, 2, 3]:', isinstance([1, 2, 3], Iterable))
print('Iterable? \'abc\':', isinstance('abc', Iterable))
print('Iterable? 123:', isinstance(123, Iterable))
print('Iterable? g():', isinstance(g(), Iterable))
print('Iterator? [1, 2, 3]:', isinstance([1, 2, 3], Iterator))
print('Iterator? iter([1, 2, 3]):', isinstance(iter([1, 2, 3]), Iterator))
print('Iterator? \'abc\':', isinstance('abc', Iterator))
print('Iterator? 123:', isinstance(123, Iterator))
print('Iterator? g():', isinstance(g(), Iterator))
结果是:
Iterable? [1, 2, 3]: True
Iterable? ‘abc’: True
Iterable? 123: False
Iterable? g(): True
Iterator? [1, 2, 3]: False
Iterator? iter([1, 2, 3]): True
Iterator? ‘abc’: False
Iterator? 123: False
Iterator? g(): True
使用生成器编写代码,代码实现功能:不断生成杨辉三角的每一行:
运行结果展示:
生成器和迭代器有什么关系?请简要说明。
迭代器:
内置有__iter__方法和__next__方法。所以:迭代器一定可迭代对象, 可迭代对象不一定是迭代器
生成器:
生成器实质就是迭代器。每次调用next()或send()就计算出下一个元素的值,直到计算出最后一个元素,没有更多的元素时,抛出StopIteration。