python - 尝试使用 python 2 枚举/循环字母表、数字等

标签 python python-2.7 if-statement enumeration

我正在尝试循环浏览字母表,因此它将打印“0,1,2,3”和“a,b,c”和“!”等等。循环完所有字符后,我想让它变成“aa”、“ab”和“a0”等等。这是我迄今为止的工作代码:

alph = {
    0: '0',
    1: '1',
    2: '2',
    3: '3',
    4: '4',
    5: '5',
    6: '6',
    7: '7',
    8: '8',
    9: '9',
    10: 'a',
    11: 'b',
    12: 'c',
    13: 'd',
    14: 'e',
    15: 'f',
    16: 'g',
    17: 'h',
    18: 'i',
    19: 'j',
    20: 'l',
    21: 'm',
    22: 'n',
    23: 'o',
    24: 'p',
    25: 'q',
    26: 'r',
    27: 's',
    28: 't',
    29: 'u',
    30: 'v',
    31: 'w',
    32: 'x',
    33: 'y',
    34: 'z',
    35: '!'
}


def one(sweet):
    print sweet

def yeah():
    i = 0
    while 1==1:
        if divmod(i,36)[0] == 0:
            a = alph[divmod(i, 36)[1]]
            sweet = a
            one(sweet)
            i += 1

        elif divmod(i,36)[0] < 36:
            b = alph[divmod(i, 36)[1]]
            a = alph[divmod(i, 36)[0]]
            sweet = a + b
            one(sweet)
            i += 1

    return false

yeah()

这部分效果很好!它将打印出“a”到“!!”。我很难理解的部分是第三部分:

        elif divmod(i,36)[0] < 36**2:
            c = alph[divmod(i, 36)[1]]
            b = alph[divmod((i//36), 36)[0]]
            a = alph[divmod(i, 36)[0]]
            sweet = a + b + c
            one(sweet)
            i += 1

这应该打印 'aaa' 'aab' 等等。我不知道该怎么做。这样做之后。我还意识到,我必须创建无限数量的“elif”语句,一个用于“aaaa”,另一个用于“aaaaa”等等。创建一个可能趋于无穷大的函数的最佳方法是什么?

最佳答案

不需要使用字典来保存基本数字,我们可以将它们放入字符串中:

alph = '0123456789abcdefghijlmnopqrstuvwxyz!'

我们可以通过在循环中进行余数除法来得到正确的数字。如果输入数字为零,则循环不会产生任何数字,因此我们将其作为特殊情况处理。此代码适用于任意长度的 base_digits 字符串,但我将仅使用短字符串来保持输出简短。此代码可以在 Python 2 和 Python 3 上正常运行。

from __future__ import print_function

def int_to_base(n, base_digits):
    if n == 0:
        return base_digits[0]
    base = len(base_digits)
    digits = []
    # Build a list of digits in reverse order
    while n:
        n, r = divmod(n, base)
        digits.append(base_digits[r])
    # Reverse the digits and join them into a string
    return ''.join(digits[::-1])

base_digits = '0ab'
for i in range(28):
    print(i, int_to_base(i, base_digits))

输出

0 0
1 a
2 b
3 a0
4 aa
5 ab
6 b0
7 ba
8 bb
9 a00
10 a0a
11 a0b
12 aa0
13 aaa
14 aab
15 ab0
16 aba
17 abb
18 b00
19 b0a
20 b0b
21 ba0
22 baa
23 bab
24 bb0
25 bba
26 bbb
27 a000

另一种方法是创建一个使用基数进行计数的生成器。您可以在 for 循环中循环生成器,或使用 next 函数获取其下一个值。

def base_counter(base_digits):
    """ An infinite iterator that counts using base_digits as its digits """
    base = len(base_digits)
    digits = [0]
    while True:
        yield ''.join([base_digits[d] for d in reversed(digits)])
        digits[0] += 1
        pos = 0
        while digits[pos] == base:
            digits[pos] = 0
            pos += 1
            if pos == len(digits):
                digits.append(1)
            else:
                digits[pos] += 1

base_digits = '0ab'
counter = base_counter(base_digits)
for i, v in enumerate(counter):
    print(i, v)
    if i == 27:
        break
print('next', next(counter))

这会产生与之前版本相同的输出,然后打印

next a00a

关于python - 尝试使用 python 2 枚举/循环字母表、数字等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52508757/

相关文章:

python - 引用的字典键是绝对必须的吗?

Python - 分割文件

R cumsum 与 if 条件

MySQL 5.5 基于IF语句的查询

python - 如何在 Flask 上运行子进程

python - 删除文件夹中的文件和子文件夹

python - 测量文本的宽度(Python/PIL)

python-2.7 - ndarray的Numpy堆栈列表

python - 如何将 jupyter 内核从 Python 2 更改为 python 3?

java - try catch 循环问题