简体   繁体   中英

All strings with list of characters?

I'm making a specialized utility similar to John the Ripper, and I'd like to use a loop that returns all strings up to x characters that can be formed from the string. For example, if the "seed" string is abcd , it should return:

a
b
c
d
aa
ab
ac

and so on. If the character limit is 10, it would generate aaaaaaaaaa , abcddcbaaa , and so on. Is there a simple for loop to do this, or is it more complicated than that?

I'll self-plagiarize from this answer and add a maximum length:

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)

which gives

>>> 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']

and so on.

As pointed out in the comment's use itertools.premutations or even better take a look @DSM's answer, as this one misses the doubles:

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']

Anyway, here's a version of @DSM's answer that returns a list:

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)]
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

Use itertools.permuataions.

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

The example code sequence generates:

[('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')]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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