Python中内置函数zip非常有用,使用该函数能将列表序列,依次取出组成一个由元祖组成的列表
>>> help(zip)
Help on built-in function zip in module __builtin__:
zip(...)
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
Return a list of tuples, where each tuple contains the i-th element
from each of the argument sequences. The returned list is truncated
in length to the length of the shortest argument sequence.
如果有两个列表,如果要组成[[10, 20], [30, 40], [50, 60]]这种数据结构,就要借助zip和自定义函数
L1 = [10, 30, 50]
L2 = [20, 40, 60]
>>> L1 = [10, 30, 50]
>>> L2 = [20, 40, 60]
>>> L3 = zip(L1, L2)
>>> L3
[(10, 20), (30, 40), (50, 60)]
>>> def handle(lst):
... tmp = []
... for item in lst:
... tmp.append(list(item))
... return tmp
...
>>> handle(L3)
[[10, 20], [30, 40], [50, 60]]
>>>