zip in python

来源:互联网 发布:蚁群算法解决背包问题 编辑:程序博客网 时间:2024/05/23 14:55
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:

>>>
>>> x = [1, 2, 3]>>> y = [4, 5, 6]>>> zipped = zip(x, y)>>> zipped[(1, 4), (2, 5), (3, 6)]>>> x2, y2 = zip(*zipped)>>> x == list(x2) and y == list(y2)True
原创粉丝点击