压缩两个列表并加入它们的元素

时间:2015-11-24 06:46:19

标签: python list

我想加入此列表:

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
merged = ['1a', '2b', '3c']

list1list2的大小始终相同。

6 个答案:

答案 0 :(得分:8)

这适用于你提到过的案例:

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
list3 = [str(x[0]) + x[1] for x in zip(list1, list2)]

答案 1 :(得分:5)

这是解决方案

list1=[1, 2, 3]  
list2=['a', 'b', 'c']

[str(list1[i])+list2[i] for i in xrange(len(list1))]

这将输出:['1a', '2b', '3c']

答案 2 :(得分:5)

我更喜欢:

>>> list1 = [1, 2, 3]
>>> list2 = ['a', 'b', 'c']
>>> [''.join(map(str, i)) for i in zip(list1, list2)]
['1a', '2b', '3c']
>>> 

答案 3 :(得分:3)

map(lambda x,y: str(x)+y,list1,list2) 

这将输出:['1a', '2b', '3c']

答案 4 :(得分:1)

我更喜欢:

>>> list1 = [1, 2, 3]
>>> list2 = ['a', 'b', 'c']
>>> [str(i) + str(j) for i, j in zip(list1, list2)]
['1a', '2b', '3c']

答案 5 :(得分:1)

你可以在功能上做到:

list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
merged = ['1a', '2b', '3c']


print(list(map("".join, zip(map(str,list1), list2))))
['1a', '2b', '3c']