如何在Python 3中使用过滤器、映射和精简
filter
, map
reduce
>>> def f(x): return x % 2 != 0 and x % 3 != 0>>> filter(f, range(2, 25))[5, 7, 11, 13, 17, 19, 23]>>> def cube(x): return x*x*x>>> map(cube, range(1, 11))[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]>>> def add(x,y): return x+y>>> reduce(add, range(1, 11))55
>>> filter(f, range(2, 25))<filter object at 0x0000000002C14908>>>> map(cube, range(1, 11)) <map object at 0x0000000002C82B70>>>> reduce(add, range(1, 11))Traceback (most recent call last): File "<pyshell#8>", line 1, in <module> reduce(add, range(1, 11))NameError: name 'reduce' is not defined
慕森卡
慕的地8271018
慕婉清6462132
相关分类