2024-10-04 07:30:03 发布
网友
如何将两个元素并排连接在一起?例如:
list1 = ["they" , "are" ,"really" , "angry"] list2 = ["they" , "are" ,"seriously" , "angry"]
我希望输出为:
上面看起来像一个列表元组,如果上面的列表是一行中每个单词的列,我如何将list2附加到list1?在
这是另一种解决方案
>>> [ (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')]
这是另一种解决方案
顺便说一下,zip()是一个很好的解决方案。在
使用^{} :
相关问题 更多 >
编程相关推荐