l1 = [4, 6, 8]
l2 = [a, b, c]
result = [(4,a),(6,b),(8,c)]
How do I do that?
解决方案
Use zip.
l1 = [1, 2, 3]
l2 = [4, 5, 6]
>>> zip(l1, l2)
[(1, 4), (2, 5), (3, 6)]
Note that if your lists are of different lengths, the result will be truncated to the length of the shortest input.
>>> print zip([1, 2, 3],[4, 5, 6, 7])
[(1, 4), (2, 5), (3, 6)]
You can also use zip with more than two lists:
>>> zip([1, 2, 3], [4, 5, 6], [7, 8, 9])
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
If you have a list of lists, you can call zip using an asterisk:
>>> l = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
>>> zip(*l)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]