+ 1
Python Shifter Code
Does anyone have any suggestions for this code? https://code.sololearn.com/cZIrurdYomLe/?ref=app
5 Answers
+ 3
Python
A list comprehension looks like this in its most basic form:
my_list = [x for x in range(1, 6)]
This creates a list from an iterable (which is a range of numbers in this case).
You can do comprehension for tuple, set or dict by changing the brackets type.
Python will know this is a dict because I used {} curly braces and two values separated by : colon.
To explain the key: ord function converts a character to its ASCII code ('a' = 65) then I add n from the looped range (0 to 25) and convert the number back to char with the chr function.
The corresponding dict value is n+1.
+ 3
You don't need to list each letter one by one. Just use a dict comprehension like this to get the same result.
w = {chr(n+ord('a')): n+1 for n in range(26)}
Edit: fixed the order of key/value
+ 1
Thanks, that makes much more sense now
0
Wait, you can do that?
0
I'm not completely sure I understand that code, could you explain it please?