[英]creating a list of all possible combination from a given list of words in python
i have a problem to create a list of all possible combinations of a given list of words.我在创建给定单词列表的所有可能组合的列表时遇到问题。 the result should be a combination per line for all possible words.
结果应该是每行所有可能单词的组合。 the max lengh of combination is based on the amount of words given in the input file.
组合的最大长度基于输入文件中给出的单词数量。 this means, if the file contains 7 words, the combination is max 7 words long.
这意味着,如果文件包含 7 个单词,则组合最长为 7 个单词。 the output should be formated like shown below:
output 的格式应如下所示:
germany germanygermany germanygeranygermany germanyspain germanygermanyspain germanygermanyspain德国德国德国德国德国德国德国德国西班牙德国德国德国德国西班牙
etc etc.等等等等
i've googled a bit and figured out, that itertools would be a possible solution for me.我用谷歌搜索了一下,发现 itertools 对我来说是一个可能的解决方案。
the given words are located in a file called input.txt给定的单词位于名为 input.txt 的文件中
i used this code from the Stack overflow entry here:我在这里使用了堆栈溢出条目中的这段代码:
How to get all possible combinations of a list's elements? 如何获得列表元素的所有可能组合?
i just represent the main part as the file read part and file output is not part of the problem here.我只是将主要部分表示为文件读取部分,文件 output 不是这里问题的一部分。
so my given list of words is: germany spain albania.netherlands所以我给出的单词列表是:德国西班牙阿尔巴尼亚荷兰
which works fine哪个工作正常
from itertools import combinations
features = ['germany', 'spain', 'albania']
tmp = []
for i in range(len(features)):
oc = combinations(features, i + 1)
for c in oc:
tmp.append(list(c))
print (tmp)
The output is not as expected. output 与预期不符。
as my list contains 3 words i changed the code:因为我的列表包含 3 个词,所以我更改了代码:
germany spain albania德国 西班牙 阿尔巴尼亚
which works fine哪个工作正常
from itertools import combinations
features = ['germany', 'spain', 'albania']
tmp = []
for i in range(len(features)):
oc = combinations(features, i + 1)
for c in oc:
tmp.append(list(c))
print (tmp)
but, i believe the result is not as expected, it should be ALL possible combinations.但是,我相信结果并不像预期的那样,它应该是所有可能的组合。 some combinations are missing, for example:
缺少一些组合,例如:
germany
germany germany
germany germany spain
germany germany germany
or something.或者其他的东西。
(the output is limited to 3 as the given list contains 3 words in the original question). (output 限制为 3,因为给定的列表在原始问题中包含 3 个单词)。
How do i get the germany germany etc combinations to the output and why are they missing?我如何将 germany germany 等组合获取到 output,为什么它们不见了? i believe i should have the same issues when i use numbers as pins or something.
我相信当我使用数字作为别针或其他东西时,我应该有同样的问题。 it cant start at 0 to 9999 but there should be a 00 and 000 and 0000 also in the list.
它不能从 0 到 9999 开始,但列表中也应该有 00、000 和 0000。
best regards Fred最好的问候弗雷德
I believe you want to use the function combinations_with_replacement :我相信您想使用 function combinations_with_replacement :
from itertools import combinations_with_replacement
features = ['germany', 'spain', 'albania']
tmp = []
for i in range(len(features)):
oc = combinations_with_replacement(features, i + 1)
for c in oc:
tmp.append(list(c))
print (tmp)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.