Python的匿名函数和map:
from random import randint def func(x): return x * 2 + 1 if __name__ == '__main__': alist = [randint(1, 100) for i in range(10)] print(alist) # map将第二个参数中的每一项交给func函数进行加工,保留加工后的结果 result = map(func, alist) # 使用常规则函数作为参数 result2 = map(lambda x: x * 2 + 1, alist) # 使用匿名函数作为参数 print(list(result)) print(list(result2))
结果输出:
[16, 27, 51, 2, 20, 8, 98, 56, 65, 72] [33, 55, 103, 5, 41, 17, 197, 113, 131, 145] [33, 55, 103, 5, 41, 17, 197, 113, 131, 145]