将每个子列表项与每个其他子列表中的每个项相匹配(python)

将每个子列表项与每个其他子列表中的每个项相匹配(python),python,python-3.x,loops,multidimensional-array,itertools,Python,Python 3.x,Loops,Multidimensional Array,Itertools,我有一个单板列表,单板电路板包含许多子列表,每个子列表中都有相同类型的电路板。本质上:[…],[…],…] 假设第一个子列表为1,第二个子列表为2。我需要将1的每个元素与2的每个元素进行比较。所以,我需要对(1[0],2[0]),(1[0],2[1])…(1[0],2[len(2)-1]);(1[0],2[0])… 问题是,我不知道板中有多少子列表,这意味着我不能只对循环执行n。这就是我现在拥有的: for sublist in boards: for board in sublist:

我有一个单板列表,
单板
<代码>电路板包含许多子列表,每个子列表中都有相同类型的电路板。本质上:
[…],[…],…]

假设第一个子列表为1,第二个子列表为2。我需要将1的每个元素与2的每个元素进行比较。所以,我需要对
(1[0],2[0]),(1[0],2[1])…(1[0],2[len(2)-1]);(1[0],2[0])…

问题是,我不知道
板中有多少子列表,这意味着我不能只对
循环执行n
。这就是我现在拥有的:

for sublist in boards:
    for board in sublist:
        for board_indices in itertools.permutations(range(len(sublist)), len(boards)):
            matched_boards = [boards[a][j] for a, j in enumerate(i)]

但我觉得我想得太多了。我确信有一种更容易、更简单、更易读的方法可以做到这一点,但我不确定它是什么。

如果您只需要对,您可以将
itertools.combinations
itertools.product
结合起来,以给出每个可能的交叉子列表对:

for sublist_pair in itertools.combinations(nested_iter, 2):
    for item_pair in itertools.product(*sublist_pair):
        print(item_pair)
给予:

(1, 'a')
(1, 'b')
(1, 'c')
(2, 'a')
(2, 'b')
(2, 'c')
(3, 'a')
(3, 'b')
(3, 'c')
(1, 0.1)
(1, 0.2)
(1, 0.3)
(2, 0.1)
(2, 0.2)
(2, 0.3)
(3, 0.1)
(3, 0.2)
(3, 0.3)
('a', 0.1)
('a', 0.2)
('a', 0.3)
('b', 0.1)
('b', 0.2)
('b', 0.3)
('c', 0.1)
('c', 0.2)
('c', 0.3)

你的问题有点不清楚。是否要从
板中的每对子列表生成每对项目?此外,您发布的代码有点奇怪。您的
for board_index
循环对
board_index
中生成的排列没有任何作用,并且您有一个未定义的变量
i
。@PM2Ring Yes,这是正确的。另外,对于
i
变量,我很抱歉,我试图使我的代码更具可读性,但忘了将
i
替换为
board\u index
。我怀疑您的第二个解决方案是OP想要的,但我想我们只能等着瞧了。:)