简体   繁体   English

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

[英]Trying to enumerate/cycle through the alphabet, numbers, and etc with python 2

I am attempting to cycle through the alphabet so it will print '0, 1, 2, 3' and 'a, b, c' and '!'我试图在字母表中循环,所以它会打印 '0, 1, 2, 3' 和 'a, b, c' 和 '!' and so on.等等。 After all the characters have been cycled through, I want to then have it go 'aa' 'ab' and 'a0' and so on.在循环完所有字符后,我想让它变成 'aa' 'ab' 和 'a0' 等等。 This is the working code I have so far:这是我到目前为止的工作代码:

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

This part works great!这部分效果很好! it will print out 'a' through '!!'.它将打印出 'a' 到 '!!'。 The part I'm struggling to wrap my head around is the third part:我正在努力解决的部分是第三部分:

        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

This should print 'aaa' 'aab' and so on.这应该打印 'aaa' 'aab' 等等。 I am not sure how to go about this.我不知道该怎么做。 After doing this.这样做之后。 I also realized that I would have to create an infinite amount of 'elif' statements, one for 'aaaa' another for 'aaaaa' and etc. What would be the best way to go about creating a function that could potentially go to infinity?我还意识到我将不得不创建无限数量的“elif”语句,一个用于“aaaa”,另一个用于“aaaaa”等等。创建一个可能达到无穷大的函数的最佳方法是什么?

There's no need to use a dict to hold the base digits, we can just put them into a string:不需要使用 dict 来保存基本数字,我们可以将它们放入一个字符串中:

alph = '0123456789abcdefghijlmnopqrstuvwxyz!'

We can get the correct digits by doing the division with remainder in a loop.我们可以通过在循环中用余数进行除法来获得正确的数字。 If the input number is zero, the loop won't produce any digits, so we handle that as a special case.如果输入数字为零,循环将不会产生任何数字,因此我们将其作为特殊情况处理。 This code will work with a base_digits string of any length, but I'll just use a short string to keep the output short.这段代码适用于任意长度的base_digits字符串,但我将只使用一个短字符串来保持输出简短。 This code works correctly on Python 2 and Python 3.此代码在 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))

output输出

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

Another way to do this is to create a generator that counts using the base digits.另一种方法是创建一个使用基本数字进行计数的生成器。 You can loop over the generator in a for loop, or fetch its next value using the next function.您可以在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))

This produces the same output as the previous version, and then it prints这产生与先前版本相同的输出,然后打印

next a00a

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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