我已经尝试了很多解决方案来解决我的问题,但我还没有找到解决方案。
import itertools
a = ['car','boat','plane']
b = list(itertools.permutations(a,))
print(b)
它在打印时给出了这个列表。
[('car', 'boat', 'plane'), ('car', 'plane', 'boat'), ('boat', 'car', 'plane'), ('boat', 'plane', 'car'), ('plane', 'car', 'boat'), ('plane', 'boat', 'car')]
但这对我来说不可用。我需要这样的东西:
car boat plane
car plane boat
boat car plane
boat plane car
plane car boat
plane boat car
单词之间有空格,没有逗号,每行 1 个。
这只是一个示例,因为我有每个 20 个单词的短语,我需要用每个短语来做这个。
现在,我要做的只是一个真实的样本。
import itertools
'phrase What are you doing to this cat?'
a = ['what','are','you','doing','to','this','cat?']
b = list(itertools.permutations(a,))
print(b)
结果将是一个怪物。