Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在3个字符串列表中生成所有可能的元素组合?_Python_String_Combinations_Permutation - Fatal编程技术网

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

Python 在3个字符串列表中生成所有可能的元素组合?,python,string,combinations,permutation,Python,String,Combinations,Permutation,我需要生成每个字符串列表中每个元素的所有可能组合 list1 = ['The girl', 'The boy'] list2 = ['wears', 'touches', 'tries'] list3 = ['a red sweater', 'a blue sweater', 'a yellow sweater', 'a white sweater'] 因此,结果是一个字符串列表,该字符串组合了每个元素和其他元素: The girl wears a red sweater The boy we

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

list1 = ['The girl', 'The boy']
list2 = ['wears', 'touches', 'tries']
list3 = ['a red sweater', 'a blue sweater', 'a yellow sweater', 'a white sweater']
因此,结果是一个字符串列表,该字符串组合了每个元素和其他元素:

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

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)
产出

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

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

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)
产出

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
使用,笛卡尔积的便捷工具。然后是产品:

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']
使用,笛卡尔积的便捷工具。然后是产品:

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的产品,其操作非常简单:

import itertools
["{x} {y} {z}".format(x=x,y=y,z=z) for x,y,z in itertools.product(list1, list2, list3)]
在Python3.6中,您可以放弃
format
调用

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

使用itertools的产品,其操作非常简单:

import itertools
["{x} {y} {z}".format(x=x,y=y,z=z) for x,y,z in itertools.product(list1, list2, list3)]
在Python3.6中,您可以放弃
format
调用

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

我试过了,但在路上弄糊涂了。非常感谢。我试过了,但在路上弄糊涂了。非常感谢。