加入2个列表,Python

2024-10-04 07:30:03 发布

您现在位置:Python中文网/ 问答频道 /正文

如何将两个元素并排连接在一起?例如:

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

我希望输出为:

^{pr2}$

上面看起来像一个列表元组,如果上面的列表是一行中每个单词的列,我如何将list2附加到list1?在


Tags: 元素列表单词are元组reallytheylist2
2条回答

这是另一种解决方案

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

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

使用^{}

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

相关问题 更多 >