我有两个清单。两个列表都具有相同的行集。我想将list2列添加到list1以创建一个列表。
list1 = [('gi1','1','2'),
('gi1','1','2'),
('gi1','1','2')]
list2 = [('a','b','c','d','e','f','g'),
('a','b','c','d','e','f','g'),
('a','b','c','d','e','f','g')]
我想将这些合并到一个如下所示的列表中:
[('gi1','1','2','a','b','c','d','e','f','g'),
('gi1','1','2','a','b','c','d','e','f','g'),
('gi1','1','2','a','b','c','d','e','f','g')]
答案 0 :(得分:2)
如果您知道两个列表的长度相同:
[list1[i] + list2[i] for i in range(len(list1))]
答案 1 :(得分:2)
我会使用itertools.chain
的帮助>>> list1=[('gi1','1','2'),
('gi1','1','2'),
('gi1','1','2')]
>>> list2=[('a','b','c','d','e','f','g'),
('a','b','c','d','e','f','g'),
('a','b','c','d','e','f','g')]
>>> from itertools import chain
>>> [tuple(chain(x, y)) for x, y in zip(list1, list2)]
[('gi1', '1', '2', 'a', 'b', 'c', 'd', 'e', 'f', 'g'), ('gi1', '1', '2', 'a', 'b', 'c', 'd', 'e', 'f', 'g'), ('gi1', '1', '2', 'a', 'b', 'c', 'd', 'e', 'f', 'g')]
答案 2 :(得分:1)
另一种方法。使用Zip()
>>> [e1+e2 for e1,e2 in zip(list1,list2)]
[('gi1', '1', '2', 'a', 'b', 'c', 'd', 'e', 'f', 'g'), ('gi1', '1', '2', 'a', 'b', 'c', 'd', 'e', 'f', 'g'), ('gi1', '1', '2', 'a', 'b', 'c', 'd', 'e', 'f', 'g')]