2016-11-30 86 views
0

我有一个计数器:如何替换.replace()的整数

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", line 99, in >count_decoder count = [w.replace(1, 'a') for w in count] AttributeError: 'int' object has no attribute 'replace'

还有什么我能做的,或者如果没有的话我该如何改变整数转换成字符串?

+1

你为什么试图模拟一个'字典()'用'名单()'? –

+0

您是否试过[map](http://book.pythontips.com/en/latest/map_filter.html#map)功能? –

回答

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'] 
相关问题