1. map(function, sequence, *sequence_1)
function:是一个函数或者一个方法,这个函数和方法需要一个参数
sequence:是一个列表,这个列表中的元素都是这个
map将sequence中的元素逐个放入function中,这样function将一个一个返回执行之后的值,map将这些值生成一个新的列表
我们可以认为,sequence经过function之后,全部变成了另一个新列表,列表的元素个数和sequence一致.
以下是python文档中的内容:
Return a list of the results of applying the function to the items of the argument sequence(s). If more than one sequence is given, the function is called with an argument list consisting of the corresponding item of each sequence, substituting None for missing values when not all sequences have the same length. If the function is None, return a list of the items of the sequence (or a list of tuples if more than one sequence).
2、zip 作用:可以用作并行迭代,可以把连个序列“压缩”在一起,然后返回一个元祖列表
names = ['anne','beth','george','damon']
ages = [12,45,32,102]
>>>zip(names,ages)
[('anne',12),('beth',45),('george',32),('damon',102)]
for name,age in zip(names,ages):
print(name,'is',age,'years old')
anne is 12 years old
beth is 45 years old
george is 32 years old
damon is 102 years old
之后,我们可以在循环中解包元祖,
zip也可以用作于任意多的序列。关于它的一个很重要的知识点是zip可以处理不等长的序列,当最短的序列用完时,就会停止
3、filter(func,seq)
返回seq中元素其函数func为真的元素形成的新列表
4、reduce(func,seq【,initial】)
等同于func(func(func(seq[0],seq[1]),seq[2]),....)