加入2个列表,Python

时间:2014-02-13 11:51:34

标签: python list

如何将两个元素并排加入2个列表?例如:

list1 = ["they" , "are" ,"really" , "angry"]  
list2 = ["they" , "are" ,"seriously" , "angry"] 

我希望输出为:

list3 = [("they","they"),("are","are"),("really","seriously"),("angry","angry")]

上面看起来像列表元组,如果上面的列表是连续每个单词的列,我如何将list2追加到list1?

2 个答案:

答案 0 :(得分:10)

使用zip()

>>> list1 = ["they" , "are" ,"really" , "angry"]  
>>> list2 = ["they" , "are" ,"seriously" , "angry"] 
>>> list3 = zip(list1, list2)
>>> list3
[('they', 'they'), ('are', 'are'), ('really', 'seriously'), ('angry', 'angry')]

答案 1 :(得分:3)

这是另一种解决方案,

>>> [ (val,list2[idx]) for idx, val in enumerate(list1)]
[('they', 'they'), ('are', 'are'), ('really', 'seriously'), ('angry', 'angry')]

顺便说一句zip()是一个很好的解决方案。