开发者

combine list elements

How can I merge/combine two or three elements of a list. For instance, if there are two elements, the list 'l'

l = [(a,b,c,d,e),(1,2,3,4,5)]

is merged 开发者_Go百科into

[(a,1),(b,2),(c,3),(d,4),(e,5)]

however if there are three elements

l = [(a,b,c,d,e),(1,2,3,4,5),(I,II,II,IV,V)] 

the list is converted into

[(a,1,I),(b,2,II),(c,3,III),(d,4,Iv),(e,5,V)]

Many thanks in advance.


Use zip:

l = [('a', 'b', 'c', 'd', 'e'), (1, 2, 3, 4, 5)]
print zip(*l)

Result:

[('a', 1), ('b', 2), ('c', 3), ('d', 4), ('e', 5)]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜