Python 列出矩阵串联

Python Lists Matrix Concatenation

有 2 个列表,长度可以不同或相同。

a = [('1','1','2'), ('2','2','2','2'), ('3','3')]
b = [('7','8','8','9'), ('0','0','4','5')]

如何添加/连接列表以便获得以下输出。

另外,不确定我在这里使用的术语是否正确。感谢任何更正。

c = [
('1','1','2','7','8','8','9'),('1','1','2','0','0','4','5'),
('2','2','2','2','7','8','8','9'),('2','2','2','2','0','0','4','5'),
('3','3','7','8','8','9'),('3','3','0','0','4','5')
]

本质上,c = [ (a[0] + b[0]), (a[0] + b[1]), (a[1] + b[0]), (a[1] + b[1]).....

到目前为止,我一直在使用 for 循环。我查看了 itertools.product,但输出不正确。此外,如果我将其增加到 3 个列表,则组合会变大。

toRex 在我输入时已经为您提供了答案。

a = [('1','1','2','3','4','5','6'),
     ('2','2','2','2','2','2','2'),
     ('3','3','2','1','1','1','1')]
b = [('7','8','8','9'),
     ('0','0','4','5')]

print ([(i+j) for i in a for j in b])

输出将是:

[('1', '1', '2', '3', '4', '5', '6', '7', '8', '8', '9'), 
 ('1', '1', '2', '3', '4', '5', '6', '0', '0', '4', '5'), 
 ('2', '2', '2', '2', '2', '2', '2', '7', '8', '8', '9'), 
 ('2', '2', '2', '2', '2', '2', '2', '0', '0', '4', '5'), 
 ('3', '3', '2', '1', '1', '1', '1', '7', '8', '8', '9'), 
 ('3', '3', '2', '1', '1', '1', '1', '0', '0', '4', '5')]

输入:

a = [('1','1','2','3','4','5','6'),
     ('2','2','2','2','2','2','2'),
     ('3','3','2','1','1','1','1','5','5','5'),
     ('1','2','3')]

b = [('7','8','8','9'),
     ('0','0','4','5'),
     ('1','2')]

输出:

[('1', '1', '2', '3', '4', '5', '6', '7', '8', '8', '9'), 
 ('1', '1', '2', '3', '4', '5', '6', '0', '0', '4', '5'), 
 ('1', '1', '2', '3', '4', '5', '6', '1', '2'), 
 ('2', '2', '2', '2', '2', '2', '2', '7', '8', '8', '9'), 
 ('2', '2', '2', '2', '2', '2', '2', '0', '0', '4', '5'), 
 ('2', '2', '2', '2', '2', '2', '2', '1', '2'), 
 ('3', '3', '2', '1', '1', '1', '1', '5', '5', '5', '7', '8', '8', '9'),
 ('3', '3', '2', '1', '1', '1', '1', '5', '5', '5', '0', '0', '4', '5'),
 ('3', '3', '2', '1', '1', '1', '1', '5', '5', '5', '1', '2'), 
 ('1', '2', '3', '7', '8', '8', '9'), 
 ('1', '2', '3', '0', '0', '4', '5'), 
 ('1', '2', '3', '1', '2')]

你走在正确的轨道上,考虑使用 product

from itertools import product

a = [('1','1','2','3','4','5','6'), ('2','2','2','2','2','2','2'), ('3','3','2','1','1','1','1')]
b = [('7','8','8','9'), ('0','0','4','5')]

paired_up = product(a, b)
c = [sum(tuples, start=()) for tuples in x]  # The default start is 0, which leads to TypeErrors.

print(c)
# [('1', '1', '2', '3', '4', '5', '6', '7', '8', '8', '9'),
#  ('1', '1', '2', '3', '4', '5', '6', '0', '0', '4', '5'),
#  ('2', '2', '2', '2', '2', '2', '2', '7', '8', '8', '9'),
#  ('2', '2', '2', '2', '2', '2', '2', '0', '0', '4', '5'),
#  ('3', '3', '2', '1', '1', '1', '1', '7', '8', '8', '9'),
#  ('3', '3', '2', '1', '1', '1', '1', '0', '0', '4', '5')]

超过两个列表?将它们全部传递给 product.

d = [('0',), ('1',)]  # Let's add another list to the mix.
paired_up = product(a, b, d)  # It gets passed to `product` with the rest.
c = [sum(tuples, start=()) for tuples in paired_up]
print(c)
# [('1', '1', '2', '3', '4', '5', '6', '7', '8', '8', '9', '0'),
#  ('1', '1', '2', '3', '4', '5', '6', '7', '8', '8', '9', '1'),
#  ('1', '1', '2', '3', '4', '5', '6', '0', '0', '4', '5', '0'),
#  ('1', '1', '2', '3', '4', '5', '6', '0', '0', '4', '5', '1'),
#  ('2', '2', '2', '2', '2', '2', '2', '7', '8', '8', '9', '0'),
#  ('2', '2', '2', '2', '2', '2', '2', '7', '8', '8', '9', '1'),
#  ('2', '2', '2', '2', '2', '2', '2', '0', '0', '4', '5', '0'),
#  ('2', '2', '2', '2', '2', '2', '2', '0', '0', '4', '5', '1'),
#  ('3', '3', '2', '1', '1', '1', '1', '7', '8', '8', '9', '0'),
#  ('3', '3', '2', '1', '1', '1', '1', '7', '8', '8', '9', '1'),
#  ('3', '3', '2', '1', '1', '1', '1', '0', '0', '4', '5', '0'),
#  ('3', '3', '2', '1', '1', '1', '1', '0', '0', '4', '5', '1')]