+ 8
[PythonEdu] IP sorting with lambda
Another problem-solving code - IP sorting. We have a list of IP addresses parsed from the net and they are stored as strings, like '192.168.1.1', '10.1.1.1', '23.44.123.73', etc. We want to sort them in ascending order, treating them as numbers - from 0.0.0.0 to 255.255.255.255 Sorting them as strings won't do - '192.x.x.x' will come before '23.x.x.x'. We have to be smart and tricky about it :) Take a look at the code here and let me know how it works for you: https://code.sololearn.com/cJqu6Q00Gidt/#py
2 Respuestas
+ 6
What it does it sorts the list based on an obtained key. Now, how to calculate the key?
1. split each item into parts (IP chunks)
2. add '00' at the beginning of each of them
3. take last three chars from each of them for further consideration (3-digit figures will be taken as they are, while 2- and 1-digit - with relevant zeros in front of them)
4. join the chunks into 12-char strings, i.e. 192168001001, 023044123073, etc.
5. such a string is ready to act as the sorting key
0
Impressive, I don't even understand what is going on in your code