Python:循环通过字母表

时间:2015-05-16 17:13:16

标签: python string loops scripting alphabet

我正在创建一个在字母表中循环的脚本,之后会添加一个再次循环的新字母。 到目前为止,这是我的代码:

import string
for a in (string.ascii_letters + string.digits):
    print (a)
for a in (string.ascii_letters + string.digits):
    for b in (string.ascii_letters + string.digits):
        print(a + b)
for a in (string.ascii_letters + string.digits):
    for b in (string.ascii_letters + string.digits):
        print(a + b)
        for c in (string.ascii_letters + string.digits):
            print(a + b + c)

更有效的方法是什么?

5 个答案:

答案 0 :(得分:2)

itertools模块将允许您定义此类字符串的无限流。

import string
import itertools

def alphanumeric_strings():
    for n in itertools.count():
        for t in itertools.product(string.ascii_letters + string.digits, repeat=n):
            yield ''.join(t)

for x in alphanumeric_strings():
    print x

count只生成一个从1开始计数的无限整数流,这样每次调用product时,都会得到一个元组流,其数量比最后一个多一个。对join的调用会将('a', 'B', '1')之类的元组转换为字符串aB1

答案 1 :(得分:1)

您可以使用itertools.product,它“等同于生成器表达式中的嵌套for循环”:

import itertools
import string
ALPHABET = string.ascii_letters + string.digits
for a in itertools.product(ALPHABET, repeat=1):
    print(a)
for a,b in itertools.product(ALPHABET, repeat=2):
    print(a + b)
# etc.

答案 2 :(得分:1)

使用itertools:

import string
from itertools import count, product

def words(chars=string.ascii_letters + string.digits):
    for n in count(1):
        yield from map(''.join, product(chars, repeat=n))

不使用itertools:

import string

def words(chars=string.ascii_letters + string.digits):
    yield from chars
    for word in words(chars):
        for char in chars:
            yield word + char

两者都产生由给定字符组成的所有单词:

for word in words('aB3'):
    print(word)

打印:

a
B
3
aa
aB
a3
Ba
BB
B3
3a
3B
33
aaa
aaB
...

答案 3 :(得分:0)

另一种方式:

abc = {0:"", 1:"", 2:""}

end = ""

for i in range(len(abc)):

    for letter in …:

        abc[i] = letter

        end = abc[0] + abc[1] + abc[2]

        print(end)

没有itertools和可变数量字母的简单方法。只需添加到abc词典。

答案 4 :(得分:0)

以下适用于Python 2和3。

# For Python 2/3 compatibility
from __future__ import print_function

import itertools
import string


def all_words(alphabet, max_length=None):
    if max_length:
        count_range = range(1, max_length+1)
    else:
        count_range = itertools.count(1)

    for length in count_range:
        for chars in itertools.product(alphabet, repeat=length):
            yield ''.join(chars)

# Demonstration

def main():
    demo_alphabet = 'abc'
    print(list(all_words(demo_alphabet, 3)))

    print()

    alphabet = string.ascii_letters + string.digits
    print(list(itertools.islice(all_words(alphabet, 2), 124)))


if __name__ == '__main__':
    main()

输出结果为:

['a', 'b', 'c', 'aa', 'ab', 'ac', 'ba', 'bb', 'bc', 'ca', 'cb', 'cc',
 'aaa', 'aab', 'aac', 'aba', 'abb', 'abc', 'aca', 'acb', 'acc', 'baa',
 'bab', 'bac', 'bba', 'bbb', 'bbc', 'bca', 'bcb', 'bcc', 'caa', 'cab',
 'cac', 'cba', 'cbb', 'cbc', 'cca', 'ccb', 'ccc']

['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B',
 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3',
 '4', '5', '6', '7', '8', '9', 'aa', 'ab', 'ac', 'ad', 'ae', 'af', 'ag',
 'ah', 'ai', 'aj', 'ak', 'al', 'am', 'an', 'ao', 'ap', 'aq', 'ar', 'as',
 'at', 'au', 'av', 'aw', 'ax', 'ay', 'az', 'aA', 'aB', 'aC', 'aD', 'aE',
 'aF', 'aG', 'aH', 'aI', 'aJ', 'aK', 'aL', 'aM', 'aN', 'aO', 'aP', 'aQ',
 'aR', 'aS', 'aT', 'aU', 'aV', 'aW', 'aX', 'aY', 'aZ', 'a0', 'a1', 'a2',
 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9']