+ 1

Python Shifter Code

Does anyone have any suggestions for this code? https://code.sololearn.com/cZIrurdYomLe/?ref=app

26th Feb 2019, 2:38 PM
builderdev212
builderdev212 - avatar
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.
27th Feb 2019, 4:44 AM
Tibor Santa
Tibor Santa - avatar
+ 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
26th Feb 2019, 6:22 PM
Tibor Santa
Tibor Santa - avatar
+ 1
Thanks, that makes much more sense now
27th Feb 2019, 11:20 AM
builderdev212
builderdev212 - avatar
0
Wait, you can do that?
26th Feb 2019, 7:16 PM
builderdev212
builderdev212 - avatar
0
I'm not completely sure I understand that code, could you explain it please?
26th Feb 2019, 11:23 PM
builderdev212
builderdev212 - avatar