__next__()
方法来迭代对象# map经常配合lambdas来使用
items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))
# 用于循环调用一列表的函数
def multiply(x):
return (x*x)
def add(x):
return (x+x)
funcs = [multiply, add]
for i in range(5):
value = map(lambda x: x(i), funcs)
print(list(value))
# Output:
# [0, 0]
# [1, 2]
# [4, 4]
# [9, 6]
# [16, 8]
number_list = range(-5, 5)
less_than_zero = filter(lambda x: x < 0, number_list)
print(list(less_than_zero))
# Output: [-5, -4, -3, -2, -1]
# 配置从哪个数字开始枚举
my_list = [‘apple‘, ‘banana‘, ‘grapes‘, ‘pear‘]
for c, value in enumerate(my_list, 1):
print(c, value)
# 输出:
(1, ‘apple‘)
(2, ‘banana‘)
(3, ‘grapes‘)
(4, ‘pear‘)
for item in container:
if search_something(item):
# Found it!
process(item)
break
else:
# Didn‘t find anything..
not_found_in_container()
from functools import reduce
product = reduce( (lambda x, y: x * y), [1, 2, 3, 4] )
# Output: 24
原文:https://www.cnblogs.com/haoxi/p/9716081.html