【问题标题】:Python every possible combination of a stringPython 字符串的所有可能组合
【发布时间】:2015-05-06 09:37:04
【问题描述】:

嗨,我正在使用 python,我正在尝试编写一个给定字符串的方法,它会找到该字符串的每个组合并将其附加到列表中。我会给出字符串并显示我想要的结果。

字符串:x = 'god'

结果:

lst = ['g', 'o', 'd', 'go', 'gd', 'og', 'od', 'dg', 'do', 'god', 'gdo', 'ogd', 'odg', 'dgo', 'dog']

一个字母只能在给定的字符串中出现的次数内使用,所以如果我们的字符串是'god''gg'或者'goo'等不能​​追加。如果这可以使用递归来完成,那就太好了!

【问题讨论】:

    标签: python string recursion combinations permutation


    【解决方案1】:

    使用itertools.permutations 并列出推导

    from itertools import permutations
    [''.join(j) for i in range(1,len(x) + 1) for j in  permutations(x, i)]
    

    输出

    ['g', 'o', 'd', 'go', 'gd', 'og', 'od', 'dg', 'do', 'god', 'gdo', 'ogd', 'odg', 'dgo', 'dog']
    

    【讨论】:

    • 哇!真是一场竞赛,不到几分钟就三个正确答案。漂亮的单线。
    【解决方案2】:

    使用permutations:

    from itertools import permutations
    
    x = 'god'
    
    
    perms = []
    
    for i in range(1, len(x)+1):
        for c in permutations(x, i):
            perms.append("".join(c))
    
    print(perms) 
    # ['g', 'o', 'd', 'go', 'gd', 'og', 'od', 'dg', 'do', 'god', 'gdo', 'ogd', 'odg', 'dgo', 'dog']
    

    【讨论】:

      【解决方案3】:

      您想使用itertools。从您写的内容来看,听起来您想使用itertools.permutation

      >>> import itertools
      >>> letters = 'god'
      >>> combinations = []
      >>> for i in range(len(letters)):
      ...     combinations.extend(
      ...         [''.join(x) for x in itertools.permutations(letters, i + 1)])
      >>> print(combinations)
      ['g', 'o', 'd', 'go', 'gd', 'og', 'od', 'dg', 'do', 'god', 'gdo', 'ogd', 'odg', 'dgo', 'dog']
      

      【讨论】:

        【解决方案4】:

        您在这里尝试做的是获取您传入的任何字符串的幂集。您要做的是将该字符串转换为字符列表,然后使用幂集的定义来使用简单列表扩展来创建您正在寻找的内容。

        def list_powerset(lst): # the power set of the empty set has one element, the empty set result = [[]] for x in lst: # for every additional element in our set # the power set consists of the subsets that don't # contain this element (just take the previous power set) # plus the subsets that do contain the element (use list # comprehension to add [x] onto everything in the # previous power set) result.extend([subset + [x] for subset in result]) return result

        以上代码在http://rosettacode.org/wiki/Power_set#Python找到

        【讨论】:

          【解决方案5】:
          import itertools
          
          def _itersubs(x):
              for i in range(1, len(x)+1):
                  yield from itertools.permutations(x, i)
                  # before 3.4, replace with:
                  # for y in itertools.permutations(x, i): yield y
          
          def thefuncyouwant(x):
              return list(_itersubs(x))
          

          我不确定你是否真的想要一个2 ** len(x)项目列表——对于任何不是很短的x都需要很多内存—— - 但是,这是你要求的,所以就在这里。一次产生一个项目的迭代器显然更自然,可能更可取,但只是将它包装在 list 调用中会消耗你所渴望的一样多的内存!-)

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-11-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-06-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多