【问题标题】:All strings with list of characters?所有带有字符列表的字符串?
【发布时间】:2013-02-04 19:25:59
【问题描述】:

我正在制作一个类似于 John the Ripper 的专用实用程序,并且我想使用一个循环来返回所有字符串,最多可以从字符串中形成 x 个字符。例如,如果“种子”字符串是abcd,它应该返回:

a
b
c
d
aa
ab
ac

等等。如果字符限制为 10,则会生成 aaaaaaaaaaabcddcbaaa 等。是否有一个简单的for 循环来执行此操作,还是比这更复杂?

【问题讨论】:

  • 您正在寻找itertools.permutations()
  • 这个答案here 是你心目中的那种东西吗? [很容易修改它以添加大小限制。]
  • @Lattyware 人,Python 真棒
  • @Lattyware: itertools.product(),不是吗?排列不包括重复值,所以没有'aaaaaaaa'
  • @tkbx:这两个倒退了。 abc 只有 3 的一种组合 (abc),但有 3 的 6 种排列。(不过,abc 确实有 2 的 3 种组合和 1 的 3 种组合,其中一种确实是 a)。

标签: python string loops


【解决方案1】:

我会从this answer自我抄袭并添加最大长度:

from itertools import product

def multiletters(seq, max_length):
    for n in range(1, max_length+1):
        for s in product(seq, repeat=n):
            yield ''.join(s)

给了

>>> list(multiletters("abc", 2))
['a', 'b', 'c', 'aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc']
>>> list(multiletters("abcd", 4))[:8]
['a', 'b', 'c', 'd', 'aa', 'ab', 'ac', 'ad']

等等。

【讨论】:

    【解决方案2】:
    def all_strings(alphabet, length_limit=None):
      n_letters = len(alphabet)
      length = 0
      n_strings = 1
      buf = []
      while True:
        for i in xrange(0, n_strings):
          k = i
          for j in xrange(length - 1, -1, -1):
            buf[j] = alphabet[k % n_letters]
            k /= n_letters
          yield ''.join(buf)
        length += 1
        if length == length_limit:
          break
        n_strings *= n_letters
        buf.append(alphabet[0])
    
    for s in all_strings('abcd', length_limit=4):
      print s
    

    【讨论】:

      【解决方案3】:

      正如评论中指出的那样使用itertools.premutations 或者更好地看看@DSM 的答案,因为这个错过了双打:

      In [194]: from itertools import chain, permutations
      
      In [195]: s = 'abcd'
      
      In [196]: map(''.join,chain.from_iterable(permutations(s,x) 
                                     for x in range(1,len(s)+1)))
      Out[196]: 
      ['a',
       'b',
       'c',
       'd',
       'ab',
       'ac',
       'ad',
       'ba',
       'bc',
       'bd',
        ...
       'dbca',
       'dcab',
       'dcba']
      

      无论如何,这是@DSM's answer的一个版本,它返回一个列表:

      from itertools import product
      
      def ms(seq, max_length):
          return [''.join(s) for n in range(1, max_length+1)
                             for s in product(seq,repeat=n)]
      

      【讨论】:

      • 不过,这不会给aa
      • @DMS -- 啊哈。我猜,product 就是这样 :)
      【解决方案4】:

      使用 itertools.permuataions。

      for i in range(2,4):
          tuples = itertools.permutations('abca' , i)
          print( list(tuples))
      

      示例代码序列生成:

      [('a', 'b'), ('a', 'c'), ('a', 'a'), ('b', 'a'), ('b', ' c'), ('b', 'a'), ('c', 'a'), ('c', 'b'), ('c', 'a'), ('a', ' a'), ('a', 'b'), ('a', 'c')]

      [('a', 'b', 'c'), ('a', 'b', 'a'), ('a', 'c', 'b'), ('a' , 'c', 'a'), ('a', 'a', 'b'), ('a', 'a', 'c'), ('b', 'a', 'c' ), ('b', 'a', 'a'), ('b', 'c', 'a'), ('b', 'c', 'a'), ('b', ' a', 'a'), ('b', 'a', 'c'), ('c', 'a', 'b'), ('c', 'a', 'a'), ('c', 'b', 'a'), ('c', 'b', 'a'), ('c', 'a', 'a'), ('c', 'a' , 'b'), ('a', 'a', 'b'), ('a', 'a', 'c'), ('a', 'b', 'a'), (' a', 'b', 'c'), ('a', 'c', 'a'), ('a', 'c', 'b')]

      【讨论】:

        猜你喜欢
        • 2018-09-07
        • 2012-12-03
        • 2017-03-09
        • 2022-09-27
        • 2019-02-17
        • 1970-01-01
        • 2013-10-21
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多