- zip([iterable, ...])
- This function returns a list of tuples, where the i-th tuple contains the
i-th element from each of the argument sequences or iterables. The returned
list is truncated in length to the length of the shortest argument sequence.
When there are multiple arguments which are all of the same length, zip()
is similar to map() with an initial argument of None. With a single
sequence argument, it returns a list of 1-tuples. With no arguments, it returns
an empty list.
The left-to-right evaluation order of the iterables is guaranteed. This makes possible an idiom for clustering a data series into n-length groups using zip(*[iter(s)]*n).
zip() in conjunction with the * operator can be used to unzip a list:
an example:
alp = ['a','b','c','d','e']
num = range(5)
zan = zip(alp, num)
print type(zan), zan
ua, un = zip(*zan)
print ua
print un
and the result:
<type 'list'> [('a', 0), ('b', 1), ('c', 2), ('d', 3), ('e', 4)] ('a', 'b', 'c', 'd', 'e') (0, 1, 2, 3, 4)
another example:
list_a = [3, 9, 17, 15, 19]
list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90]
zab = zip(list_a, list_b)
print type(zab), zab
and the result:
<type 'list'> [(3, 2), (9, 4), (17, 8), (15, 10), (19, 30)]
2015年9月3日 星期四
[RR Python] zip is not zip...(compression)
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言