【问题标题】:python: create all possible variations for a string with hyphenspython:为带有连字符的字符串创建所有可能的变体
【发布时间】:2020-04-24 20:21:48
【问题描述】:

我有一个连字符字符串列表:

(样本)

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


    【解决方案1】:

    制作自己的生成器来生成组合可能会更容易一些。这可以使用递归生成器以非常易读的方式完成,只要您的字符串不够大而不会遇到堆栈限制:

    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]
    

    【讨论】:

      【解决方案2】:

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

      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
      

      【讨论】:

      • 如果有人知道如何改进我制作 rtn 变量的方式,我会很高兴。为此使用 for 循环并不是很令人满意。
      猜你喜欢
      • 2013-10-10
      • 2022-01-12
      • 2013-12-21
      • 2020-10-15
      • 2015-05-06
      • 2020-09-11
      • 1970-01-01
      • 2020-06-03
      • 2021-07-10
      相关资源
      最近更新 更多