从字符列表生成字符串

时间:2015-05-05 01:30:40

标签: python generator

我有以下信件:

Letters = ["a", "b", "c", "d", "e"]

我想要的是编写一个生成器函数,它将创建可以通过组合任何字母形成的字符串,最好是从最小到最大的某些确定性顺序。

因此,例如,如果我要运行发生器20次,我会得到

 a
 b
 c
 d
 e

aa
ab
ac
ad
ae

ba
bb
bc
bd
be

ca
cb
cc
cd
ce

da

我该怎么写这个发电机?

4 个答案:

答案 0 :(得分:2)

生成器功能:

from itertools import *

def wordgen(letters):
    for n in count(1):
        yield from map(''.join, product(letters, repeat=n))

用法:

for word in wordgen('abcde'):
    print(word)

输出:

a
b
c
d
e
aa
ab
ac
ad
ae
ba
bb
bc
bd
be
ca
...

不使用itertools的自制替代方案:

def wordgen(letters):
    yield from letters
    for word in wordgen(letters):
        for letter in letters:
            yield word + letter

高尔夫版(诚然以空字符串开头):

def w(s):yield'';yield from(w+c for w in w(s)for c in s)

答案 1 :(得分:1)

使用itertools库中的组合函数。有替换和无替换的组合

for item in itertools.combinations(Letters, 2):
    print("".join(item))

https://docs.python.org/3.4/library/itertools.html

答案 2 :(得分:1)

使用itertools.product()

from itertools import product, imap
letters = ["a", "b", "c", "d", "e"]
letters += imap(''.join, product(letters, repeat=2))
print letters
  

['a','b','c','d','e','aa','ab','ac','ad','ae','ba','bb ','bc','bd','be','ca','cb','cc','cd','ce','da','db','dc','dd', 'de','ea','eb','ec','ed','ee']

答案 3 :(得分:0)

我使用递归生成器函数(没有itertools)

Letters = ["a", "b", "c", "d", "e"]

def my_generator(list, first=""):
  for letter in list:
    yield first + letter
  my_generators = []
  for letter in list:
    my_generators.append(my_generator(list, first + letter))
  i = 0
  while True:
    for j in xrange(len(list)**(i/len(list)+1)):
      yield next(my_generators[i%len(list)])
    i+=1

gen = my_generator(Letters)
[next(gen) for c in xrange(160)]

你得到了

['a', 'b', 'c', 'd', 'e', 'aa', 'ab', 'ac', 'ad', 'ae', 'ba', 'bb',
'bc', 'bd', 'be', 'ca', 'cb', 'cc', 'cd', 'ce', 'da', 'db', 'dc',
'dd', 'de', 'ea', 'eb', 'ec', 'ed', 'ee', 'aaa', 'aab', 'aac', 'aad',
'aae', 'aba', 'abb', 'abc', 'abd', 'abe', 'aca', 'acb', 'acc', 'acd',
'ace', 'ada', 'adb', 'adc', 'add', 'ade', 'aea', 'aeb', 'aec', 'aed', 
'aee', 'baa', 'bab', 'bac', 'bad', 'bae', 'bba', 'bbb', 'bbc', 'bbd',
'bbe', 'bca', 'bcb', 'bcc', 'bcd', 'bce', 'bda', 'bdb', 'bdc', 'bdd',
'bde', 'bea', 'beb', 'bec', 'bed', 'bee', 'caa', 'cab', 'cac', 'cad',
'cae', 'cba', 'cbb', 'cbc', 'cbd', 'cbe', 'cca', 'ccb', 'ccc', 'ccd',
'cce', 'cda', 'cdb', 'cdc', 'cdd', 'cde', 'cea', 'ceb', 'cec', 'ced',
'cee', 'daa', 'dab', 'dac', 'dad', 'dae', 'dba', 'dbb', 'dbc', 'dbd',
'dbe', 'dca', 'dcb', 'dcc', 'dcd', 'dce', 'dda', 'ddb', 'ddc', 'ddd',
'dde', 'dea', 'deb', 'dec', 'ded', 'dee', 'eaa', 'eab', 'eac', 'ead',
'eae', 'eba', 'ebb', 'ebc', 'ebd', 'ebe', 'eca', 'ecb', 'ecc', 'ecd', 
'ece', 'eda', 'edb', 'edc', 'edd', 'ede', 'eea', 'eeb', 'eec', 'eed',
'eee', 'aaaa', 'aaab', 'aaac', 'aaad', 'aaae']