Python基础-函数

高阶函数map(),接受一个函数和一个序列(迭代器)作为输入,然后对序列(迭代器)的每一个值应用这个函数,返回一个序列(迭代器),其包含应用函数后的结果。

1
2
3
4
5
6
7
>>> lst = [1, 2, 3, 4, 5]
>>> def square(num):
... "返回所给数字的平方."
... return num * num
...
>>> print(list(map(square, lst)))
[1, 4, 9, 16, 25]