组元组和列表维护命令python

时间:2013-08-07 11:29:30

标签: python

我有一个列表和一个元组。我知道元组不是要修改的,但我需要加入元组,并在两者之间保持顺序,例如..

l1 = [(166L,), (155L,)]
l2 = [0.74, 0.25]

我想

l3 = [[166,0.74],[155,0.25]]

我尝试排序(l1 + l2),但这不起作用

[0.25, 0.7, (155L,), (166L,)]

2 个答案:

答案 0 :(得分:3)

>>> l1 = [(166L,), (155L,)]
>>> l2 = [0.74, 0.25]
>>> zip([x for x, in l1], l2)
[(166L, 0.74), (155L, 0.25)]
>>> map(list, zip([x for x, in l1], l2))
[[166L, 0.74], [155L, 0.25]]

答案 1 :(得分:2)

l3 = [[l1[i][0], l2[i]] for i in range(len(l1))]

将起作用,假设l1由1元组成,l2由单个元素组成,l1和l2具有相同的长度。