简体   繁体   English

使用可变长度字符串的可变长度列表,如何创建所有组合

[英]With variable length list of variable length strings, how do I create all combinations

In python 3, I have a variable length list, where each element of the list is a variable length string. 在python 3中,我有一个可变长度列表,其中列表的每个元素都是可变长度字符串。 Something like this 像这样

['TO', 'G', 'ZDO', 'DEO', 'SGT', 'D', 'Z', 'FT', 'OV']

and I want to iterate over every possible combination of words where the letters that make up the word are from the strings in the list, and the length of the word is the same as the length of the list. 我想遍历单词的所有可能组合,其中组成单词的字母来自列表中的字符串,并且单词的长度与列表的长度相同。 So something like this 所以像这样

TGZDSDZFO
TGZDSDZFV
TGZDSDZTO
...
OGOOTDZTO
OGOOTDZTV

I am having trouble coming up with a generic solution for n-sized list. 我在为n大小列表提出通用解决方案时遇到了麻烦。

>>> (''.join(s) for s in itertools.product(*['TO', 'G', 'ZDO', 'DEO', 'SGT', 'D', 'Z', 'FT', 'OV']))
<generator object <genexpr> at 0x7f2a46468f00>
>>> # to demonstrate:
... 
>>> list(itertools.islice((''.join(s) for s in itertools.product(*['TO', 'G', 'ZDO', 'DEO', 'SGT', 'D', 'Z', 'FT', 'OV'])), 3))
['TGZDSDZFO', 'TGZDSDZFV', 'TGZDSDZTO']

As others have suggested, itertools is perhaps the simplest/easiest way to solve this. 正如其他人所建议的那样, itertools也许是解决此问题的最简单/最简单的方法。 If you are looking to write your own algorithm however (ie reimplement what itertools does under the hood), then take a look at this: 但是,如果您希望编写自己的算法(即重新实现itertools功能),请看一下以下内容:

def allPerms(L, sofar=''):
    if not L:
        print(sofar)
    else:
        for char in L[0]:
            allPerms(L[1:], sofar+char)

Output: 输出:

In [97]: L = ['TO', 'G', 'ZDO', 'DEO', 'SGT', 'D', 'Z', 'FT', 'OV']

In [98]: allPerms(L)
TGZDSDZFO
TGZDSDZFV
TGZDSDZTO
TGZDSDZTV
TGZDGDZFO
TGZDGDZFV
TGZDGDZTO
TGZDGDZTV
TGZDTDZFO
TGZDTDZFV
TGZDTDZTO
TGZDTDZTV
TGZESDZFO
TGZESDZFV
TGZESDZTO
TGZESDZTV
TGZEGDZFO
TGZEGDZFV
TGZEGDZTO
TGZEGDZTV
--- truncated ---

EDIT : 编辑

As @njzk2 points out , python3's yield-from does a fantastic job of making the output usable: 正如@ n​​jzk2指出的那样 ,python3的yield-from出色地完成了使输出可用的工作:

def allPerms(L, sofar=''):
    if not L: yield sofar
    else:
        for char in L[0]: yield from allPerms(L[1:], sofar+char)

Output: 输出:

In [118]: for i in allPerms(L): print(i)
TGZDSDZFO
TGZDSDZFV
TGZDSDZTO
TGZDSDZTV
TGZDGDZFO
TGZDGDZFV
TGZDGDZTO
TGZDGDZTV
TGZDTDZFO
TGZDTDZFV
TGZDTDZTO
TGZDTDZTV
TGZESDZFO
TGZESDZFV
TGZESDZTO
TGZESDZTV
TGZEGDZFO
TGZEGDZFV
TGZEGDZTO
TGZEGDZTV
TGZETDZFO
TGZETDZFV
TGZETDZTO
--- truncated ---

you could use the itertools module to create the permutations of your desired length. 您可以使用itertools模块创建所需长度的排列。 combine all the workds to one string and use it in the permutations function 将所有workds合并为一个字符串,并在排列函数中使用

lst = ['TO', 'G', 'ZDO', 'DEO', 'SGT', 'D', 'Z', 'FT', 'OV']
length = len(lst)

combined = ''.join(lst)
all_perms = itertools.permutations(combined, length)
#this will give you something like [('T', 'O', ...), (...),]

print ([''.join(x) for x in all_perms])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM