Einstein.py Einstein.py - 2 days ago 5
Python Question

How to replace an integer with .replace()

I have a counter:

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 = []


and I want to change each digit in the list to the corresponding letter (1 = a, 26 = z)

I have tried .replace() but it comes up with:


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'


What else can I do, or if not then how do I change the integer into a string?

Answer

I'm interpreting your question as, "given a list of numbers between 1 and 26, how do I get a list of characters between a and z?". You could do:

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

Example:

>>> 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']
Comments