For the input text: I would like to receive following solution: rafal
"r" 0 -5
"a" 1 -4
"f" 2 -3
"a" 3 -2
"l" 4 -1
where first column are letters given in text, second column are indexes values and third column are reverse indexes values.
I performed enumeration: for idx, znak in enumerate(tekst): print('"' znak '"', idx)
which gave me following output: daj tekst: rafal "r" 0
"a" 1
"f" 2
"a" 3
"l" 4
I do not know how to print reverse index values. When i created new string which was reversion of original text and enumerate through this:
for idx, znak in enumerate(tekst): new_txt = tekst[::-1] for r_idx, znak in enumerate(tekst): print('"' znak '"', idx, r_idx)
I receievd following output:
daj tekst: rafal "r" 0 0
"a" 0 1
"f" 0 2
"a" 0 3
"l" 0 4
"r" 1 0
"a" 1 1
"f" 1 2
"a" 1 3
"l" 1 4
"r" 2 0
"a" 2 1
"f" 2 2
"a" 2 3
"l" 2 4
"r" 3 0
"a" 3 1
"f" 3 2
"a" 3 3
"l" 3 4
"r" 4 0
"a" 4 1
"f" 4 2
"a" 4 3
"l" 4 4
How can I receive desired output ?
CodePudding user response:
You can try code below:
def getStringData(string):
array = list(string)
length = len(array)
for index, value in enumerate(array):
print("'" value "'", index, index-length)
getStringData("rafal")
Output
'r' 0 -5
'a' 1 -4
'f' 2 -3
'a' 3 -2
'l' 4 -1
CodePudding user response:
word = "rafal"
for i in range(len(word)):
print(f"{word[i]}\t{i}\t{i-len(word)}")
CodePudding user response:
You could use two levels of enumerate, with one of them starting at the first negative index:
s = 'rafal'
for n,(i,c) in enumerate(enumerate(s),-len(s)):
print(repr(c),i,n)
'r' 0 -5
'a' 1 -4
'f' 2 -3
'a' 3 -2
'l' 4 -1