如何用.replace()替换整数

时间:2016-11-30 19:16:45

标签: python python-2.7

我有一个柜台:

count = []

def count_up(digit_1, digit_2, digit_3, digit_4, digit_5, digit_6,     digit_7):
    if(tick = True):
        count = []
        digit_1 = digit_1 + 1
        if(digit_1 == 27):
            digit_1 = 0
            digit_2 += 1
            if(digit_2 == 27):
                digit_2 = 0
                digit_3 += 1
                if(digit_3 == 27):
                    digit_3 = 0
                    digit_4 += 1
                    if(digit_4 == 26):
                        digit_4 = 0
                        digit_5 += 1
                        if(digit_5 == 26):
                            digit_5 = 0

    count.append(digit_1)
    count.append(digit_2)
    count.append(digit_3)
    count.append(digit_4)
    count.append(digit_5)
    print count
    count = []

我希望将列表中的每个数字更改为相应的字母(1 = a,26 = z)

我尝试过.replace(),但它提出了:

  

File" /Users/Johnpaulbeer/pythonpractice/test.py" ;,第99行,> count_decoder      count = [w.replace(1,' a')为计数中的w]   AttributeError:' int'对象没有属性'替换'

我还能做什么,或者如果没有,那么如何将整数更改为字符串?

1 个答案:

答案 0 :(得分:4)

我将您的问题解释为,"给出1到26之间的数字列表,如何获得a和z之间的字符列表?"。你可以这样做:

count = [chr(w + ord("a") - 1) for w in count]

示例:

>>> count = [8, 5, 12, 12, 15, 23, 15, 18, 12, 4]
>>> count = [chr(w + ord("a") - 1) for w in count]
>>> count
['h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd']