python:为带有连字符的字符串创建所有可能的变体

问题描述 投票:1回答:2

我有一个带连字符的字符串列表:(样本)

myList = ['mother-in-law', 'co-operation', 'sixty-nine-eighty-ninths']

对于此列表中的每个元素,我希望能够创建所有变体,其中连字符位于每个元素的两个或多个标记之间:

mother-in law
mother in-law
sixty-nine eighty ninths
sixty-nine-eighty ninths
sixty nine-eighty-ninths
sixty-nine eighty-ninths
sixty nine-eighty ninths
sixty nine eighty-ninths
...

我尝试过此问题的解决方案(Create variations of a string)但我想不出如何适应它:

from itertools import combinations
myList = ['mother-in-law', 'co-operation', 'sixty-nine-eighty-ninths']

for e in myList :
    for i in range(len(e.split("-"))):
        for indices in combinations(range(len(e.split("-"))), i):
            print(''.join([e.split("-")[x] if x in indices else '-' for x in range(len(e))]))

这就是我得到的:

-------------
mother------------
-in-----------
--law----------
motherin-----------
mother-law----------
-inlaw----------
------------
co-----------
-operation----------
------------------------
sixty-----------------------
-nine----------------------
--eighty---------------------
---ninths--------------------
sixtynine----------------------
sixty-eighty---------------------
sixty--ninths--------------------
-nineeighty---------------------
-nine-ninths--------------------
--eightyninths--------------------
sixtynineeighty---------------------
sixtynine-ninths--------------------
sixty-eightyninths--------------------
-nineeightyninths--------------------

谢谢

python string variations
2个回答
0
投票

仅制作自己的生成器来生成组合可能会更容易一些。只要您的字符串不够大而无法进入堆栈限制,就可以使用递归生成器以一种非常易读的方式来完成此操作:

def hyphenCombos(s):
    head, _, rest = s.partition('-')
    if len(rest) == 0:
        yield head
    else:
        for c in hyphenCombos(rest):
            yield f'{head}-{c}'
            yield f'{head} {c}'

s = 'sixty-nine-eighty-ninths'
list(hyphenCombos(s))

结果:

['sixty-nine-eighty-ninths',
 'sixty nine-eighty-ninths',
 'sixty-nine eighty-ninths',
 'sixty nine eighty-ninths',
 'sixty-nine-eighty ninths',
 'sixty nine-eighty ninths',
 'sixty-nine eighty ninths',
 'sixty nine eighty ninths']

因此,您可以将其用于理解或将其传递给其他itertools函数以执行所需的任何操作:

myList = ['mother-in-law', 'co-operation', 'sixty-nine-eighty-ninths']
chain.from_iterable(hyphenCombos(s) for s in myList))
# or variations...
# [list(hyphenCombos(s)) for s in myList]

0
投票

稍微浏览一下itertools提供的工具,我发现产品在这里可能最有用。它使我们可以了解在两个单词之间使用空格或破折号的所有可能性。

from itertools import product, zip_longest

my_list = ['mother-in-law', 'co-operation', 'sixty-nine-eighty-ninths']
symbols = ' ', '-'

for string in my_list:
    string_split = string.split('-')
    for symbols_product in product(symbols, repeat=len(string_split)-1):
        if '-' not in symbols_product:
            continue
        rtn = ""
        for word, symbol in zip_longest(string_split, symbols_product, fillvalue=''):
            rtn += word + symbol
        print(rtn)
    print()

此外,根据您的要求,我也跳过了两个单词之间没有破折号的迭代。

输出:

mother in-law
mother-in law
mother-in-law

co-operation

sixty nine eighty-ninths
sixty nine-eighty ninths
sixty nine-eighty-ninths
sixty-nine eighty ninths
sixty-nine eighty-ninths
sixty-nine-eighty ninths
sixty-nine-eighty-ninths
© www.soinside.com 2019 - 2024. All rights reserved.