zip()
Summary
zip() makes an iterator that aggregates elements from each of the iterables.
zip() returns an iterator of tuples, where the i-th tuple contains the i-th element from each of the iterables (seqs). The iterator stops when the shortest input iterable is exhausted.
zip()函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的zip对象。
如果传入的可迭代对象的长度不等,则返回的对象长度和参数中长度最短的对象相同。如果参照长度最长的对象,则需使用itertools.zip_longest()。
利用*可将打包好的对象进行解压。
Examples
a = [1,2,3]
b = ['a','b','c','d']
zipped = zip(a, b)
# [(1, 'a'), (2, 'b'), (3, 'c')]
unzipped = zip(*zipped)
# [(1,2,3), ('a','b','c')]
d = dict(zipped)
# {1:'a', 2:'b', 3:'c'}
注意,zip对象只能迭代一次
print('1st iteration...')
for item in zipped:
print(item)
print('2nd iteration...')
for item in zipped:
print(item)
如果多次利用zip对象,则需将其转化为list:
a = [1,2,3]
b = ['a','b','c','d']
zipped = list(zip(a, b))
map()
Summary
map() returns an iterator that applies function to every item of iterable, yielding the results.
map()接收一个函数func()和一个可迭代对象,并通过把函数func作用在每一个对象上得到一个新的map对象并返回。
Examples
l = ['1','2','3','4']
mapped_l = map(int, l)
# [1,2,3,4]
Using independent function itself:
def f(x):
return x*2
l = [1,2,3,4]
mapped_l = map(f, l)
# [2,4,6,8]
Using lambda:
l = [1,2,3,4]
mapped_l = map(lambda x: x*2, l)
# [2,4,6,8]
Operations on two or more iterables, the operating func must be able to take two or more args:
l = [1,4,16,256]
L = [2,3,5,7]
mapped_l = map(lambda x, y: x >= y, l, L)
# [False, True, True, True]
filter()
Summary
filter() constructs an iterator from those elements of iterable for which func returns true.
iterable may be either a seq, a container which supports iteration, or an iterator.
Note that filter(func, iter) is equivalent to the generator expression:
(item for item in iter if function(item)) if funtion is not None and (item for item in iter if item) if function is None
Examples
l = [1,4,16,256]
filtered_l = filter(lambda x: x >= 10, l)
# [16,256]
Note, return values are different for filter() and map():
l = [1,4,16,256]
mapped_l = map(lambda x: x >= 10, l)
# [False,False,True,True]
reduce()
Summary
functools.reduce(func, iterable[, initializer])
Apply func of two args cumulatively to the items of seq, from left to the right, so as to reduce the seq to a single val, for example:
functools.reduce(lambda x, y: x+y, [1,2,3,4,5])
# calculates ((((1+2)+3)+4)+5)
The left arg within lambda exp x is the accumulated val and the right arg y is the next val from the seq.
If the optional initializer is present, it is placed before the items of the seq in the calculation, and serves as a default when the seq is empty. If initializer is not given and the seq contains one item, the first item is returned accordingly:
reduce(lambda x, y: x+y, [2,3,5,7], 2)
# 19
reduce(lambda x, y: x+y, [1])
# 1, 1st item returned
综上,reduce把一个函数作用在一个序列seq上,这个函数必须接收两个参数arg,reduce把结果继续和序列中的下一个元素做累积计算。
Examples
求阶乘,factorial:
n = 6
reduce(lambda x, y: x*y, range(1,n))
# 120
把序列[1,3,5,7,9]变成整数13579:
reduce(lambda x, y: x*10+y, [1,3,5,7,9])
# 13579
设计一个str转int函数
def func(s):
ref_dict = {'0':0, '1':1, '2':2, '3':3, '4':4,
'5':5, '6':6, '7':7, '8':8, '9':9, }
return reduce(lambda x, y: 10*x+y, map(lambda x: ref_dict[x], s))