关于python:在3个字符串列表中生成所有可能的元素组合?

Generate all posible combinations of elements in 3 lists of strings?

本问题已经有最佳答案,请猛点这里访问。

我需要在每个字符串列表中生成每个元素的所有可能组合

1
2
3
list1 = ['The girl', 'The boy']
list2 = ['wears', 'touches', 'tries']
list3 = ['a red sweater', 'a blue sweater', 'a yellow sweater', 'a white sweater']

因此,结果是一个字符串列表,将每个元素与其他元素组合在一起:

1
2
3
4
5
6
7
The girl wears a red sweater
The boy wears a red sweater
The girl touches a red sweater
The boy touches a red sweater
The girl wears a blue sweater
The boy wears a yellow sweater
(ETC...)

只要获得所有组合,我就不特别关心输出的顺序。

根据我的研究,我猜想"排列"是一个解决方案,但我只找到了几个关于数字列表排列或字符串中每个字母组合的答案。这些都不是我需要的。我需要组合按列表排列的文本块。

如何创建一个长的句子列表,其中包含每个字符串列表中不同元素的所有组合?

谢谢你


只需要一组简单的for循环就可以了。诀窍是打印顺序z,y,x

1
2
3
4
5
6
7
8
list1 = ['The girl', 'The boy']
list2 = ['wears', 'touches', 'tries']
list3 = ['a red sweater', 'a blue sweater', 'a yellow sweater', 'a white sweater']

for x in list3:
    for y in list2:
        for z in list1:
            print (z,y,x)

输出;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
The girl wears a red sweater
The boy wears a red sweater
The girl touches a red sweater
The boy touches a red sweater
The girl tries a red sweater
The boy tries a red sweater
The girl wears a blue sweater
The boy wears a blue sweater
The girl touches a blue sweater
The boy touches a blue sweater
The girl tries a blue sweater
The boy tries a blue sweater
The girl wears a yellow sweater
The boy wears a yellow sweater
The girl touches a yellow sweater
The boy touches a yellow sweater
The girl tries a yellow sweater
The boy tries a yellow sweater
The girl wears a white sweater
The boy wears a white sweater
The girl touches a white sweater
The boy touches a white sweater
The girl tries a white sweater
The boy tries a white sweater


使用itertools.product,这是笛卡尔积的一个方便工具。然后join产品:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
from itertools import product
lst = [' '.join(p) for p in product(list1, list2, list3)]

from pprint import pprint
pprint(lst)
['The girl wears a red sweater',
 'The girl wears a blue sweater',
 'The girl wears a yellow sweater',
 'The girl wears a white sweater',
 'The girl touches a red sweater',
 'The girl touches a blue sweater',
 'The girl touches a yellow sweater',
 'The girl touches a white sweater',
 'The girl tries a red sweater',
 'The girl tries a blue sweater',
 'The girl tries a yellow sweater',
 'The girl tries a white sweater',
 'The boy wears a red sweater',
 'The boy wears a blue sweater',
 'The boy wears a yellow sweater',
 'The boy wears a white sweater',
 'The boy touches a red sweater',
 'The boy touches a blue sweater',
 'The boy touches a yellow sweater',
 'The boy touches a white sweater',
 'The boy tries a red sweater',
 'The boy tries a blue sweater',
 'The boy tries a yellow sweater',
 'The boy tries a white sweater']


使用Itertools的产品,其简单程度如下:

1
2
import itertools
["{x} {y} {z}".format(x=x,y=y,z=z) for x,y,z in itertools.product(list1, list2, list3)]

在python 3.6中,可以删除format调用

1
[f"{x} {y} {z}" for x,y,z in itertools.product(list1, list2, list3)]