Jan-30-2021, 02:29 AM
(Jan-30-2021, 02:24 AM)bowlofred Wrote: In the first place, why are you building a dictionary that maps from position to letter, but then using it to look from letter to position? My first thought is that you should build the dictionary in that direction and use it naturally. What this program does is use the dictionary like a list. It would be simpler to just loop through the originalstring.ascii_lowercase
than loop through the dictionary.
Next, whilealphabet_position
loops over the characters in the input, it overwritesstrin
each time through the loop. So only the final character is kept in that variable.
Finally, the function has no return call. So if you are calling it, you will always get backNone
Thanks for the reply. Your first point is great and makes sense.
For your latter part of the comment, why does it overwrite? When I add the ids to some list, the list is not being overwritten.
Here's the code with adding to the list. I also added returns.
def alphabet_position(text): for i in text: lista.append([k+1 for k,v in dic.items() if v == i.lower()]) strin=' '.join([str(k+1)+v for k,v in dic.items() if v == i.lower()])