I'm taking computer science courses at my school I need to figure out some code for an exam, I need to change the variable name after I print it in a for-loop after decoding RLE. It's hard for me to explain but I hope you understand.
I've tried to look it up online but I can't find the proper way to phrase the problem I have.
import re
def decode(string):
return re.sub(r'(\d+)(\D)', lambda m: int(m.group(1)) * m.group(2), string)
ch=int(input())
for x in range(0, ch):
globals()['line%s' % x]=input()
for x in range(0, ch):
print(decode(['line%s' % x]))
The result I'm looking for is after entering the RLE line by line (ch is the amount of lines), the function I made takes the variable names created by the for loop (so their names are line0, line1, line2 etc... until line(ch)) and then proceeds to decompress and print the RLE but I cant seem to get it to work
Edit: I might have made my problem not obvious and I apologize, I want to fix the last two lines of code so that the RLE is decompressed and printed in one for loop, I don't know if this is possible but I just want to know how (if) I can somehow make print(decode(['line%s' % x])) work, so like the variable line followed by a number gets changed while in the variable. Sorry I'm terrible at explaining things.
line0,line1,line2, etc. you should just use a list. Other dynamic variable names should be dictionaries.line[0],line[1], ...line[x].