在给定每个数字或索引的选择列表的情况下,是否有生成可能排列的函数?

Is there a function to generate possible permutations given list of choices for each digit or index?

我正在尝试为最终列表的每个索引给出一个选项列表来生成可能的序列。

示例: 在下面的字典中,我有 5 个单词,每个单词可能有不同的值。 我想生成所有可能的排列或序列。

{'John': ['NNP'], 'went': ['VBD'], 'to': ['TO'], 'work': ['VBP', 'NN', 'VB'], '.': ['.']}

在此示例中,将有 1 * 1 * 1 * 3 * 1 = 3 个序列。

您想要 itertools.product 函数:

from itertools import product

arrs = [[1,2],[1],[1,2,3],[4]]

for combo in product(*arrs):
  print(combo)

输出:

(1, 1, 1, 4)
(1, 1, 2, 4)
(1, 1, 3, 4)
(2, 1, 1, 4)
(2, 1, 2, 4)
(2, 1, 3, 4)