如果你想要 64 种可能性,你可以使用itertools.product
:
>>> from itertools import product
>>> A = "dog bit dog null".split()
>>> B = "hund bet hund".split()
>>> product(A, repeat=3)
<itertools.product object at 0x1148fd500>
>>> len(list(product(A, repeat=3)))
64
>>> list(product(A, repeat=3))[:5]
[('dog', 'dog', 'dog'), ('dog', 'dog', 'bit'), ('dog', 'dog', 'dog'), ('dog', 'dog', 'null'), ('dog', 'bit', 'dog')]
但请注意,这将产生相当数量的重复,因为您有dog
两次A
:
>>> len(set(product(A, repeat=3)))
27
如果你愿意,你甚至可以得到相关的三元组:
>>> trips = [zip(B, p) for p in product(A, repeat=len(B))]
>>> trips[:5]
[[('hund', 'dog'), ('bet', 'dog'), ('hund', 'dog')], [('hund', 'dog'), ('bet', 'dog'), ('hund', 'bit')], [('hund', 'dog'), ('bet', 'dog'), ('hund', 'dog')], [('hund', 'dog'), ('bet', 'dog'), ('hund', 'null')], [('hund', 'dog'), ('bet', 'bit'), ('hund', 'dog')]]