简体   繁体   English

从 python 中的给定单词列表创建所有可能组合的列表

[英]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.

相关问题 从给定的单词列表中生成具有“N”长度的所有可能组合(寻找不重复) - Generate all possible combination with “N” length from given words list (Looking for no repeat) 给定列表的所有可能的列表组合 - All possible combination of lists for a given list Python:从具有字符限制的列表中生成所有可能的序列组合 - Python: Produce all possible sequence combination from a list with character limit 获取列表中所有可能的值组合 — Python - Get all possible combination of values in a list — Python 从列表字母中打印所有可能的长度为 10 的单词的组合,其中重复“A”两次 - Print all possible combination of words of length 10 from a list letters with repeating 'A' exactly twice 创建既在给定字符串中又在给定字典中的所有单词的列表 - Creating a list of all words that are both in a given string and in a given dictionary 如何在列表中创建给定字母组合的所有可能子集? - How to create all possible subsets of given combination of letters in a list? python从字母表中获取所有可能的单词 - python get all possible words from list of alphabets 从给定字符开始的给定长度的字符列表中创建所有可能的单词 - Create all the possible words from a list of character for a given length that starts with a given character Python:列表中每个可能的子集组合 - Python: every possible subset combination from a list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM