How does this code work?
values = ['1', '3', '9', '6', '7'] # just an example of what comes in
index = "123456789"["8" in values:].index # everything except this line is understandable
print(sorted(values, key=index))
this kind of code looks very strange to me so i don't understand how it works.
str[str in list:].index
This is needlessly complicated code. Either someone did something incorrectly, or they're trying to be clever. Here's what that line means:
index = "123456789"["8" in values:].index
Start with the "8" in values
. This evaluates to False
, because "8"
is not in the values
list. This is the same as:
index = "123456789"[False:].index
which, because False
evaluates to 0
, is just the same as:
index = "123456789"[0:].index
and because [0:]
refers to the entire string, this:
index = "123456789".index
sets the variable index
to point to the .index()
method of the string "123456789"
. This is then used as the key to sorted()
which has the effect of sorting values
based on the index of each value in the "123456789"
string.
In the end, this is the same as:
print(sorted(values))
index = "123456789"["8" in values:].index # everything except this line is understandable
There are a couple things happening on that line and we can split it into multiple lines to understand what is happening:
values = ['1', '3', '9', '6', '7'] # just an example of what comes in
boolean = ("8" in values)
numbers = "123456789"[boolean:]
index = numbers.index # everything except this line is understandable
print(sorted(values, key=index))
Let's start with "8" in values
this produces a boolean output which is False
because there is no 8
in the values
list.
Now we have "123456789"[False:]
In python strings can be accessed like an array with [ ] brackets.
Let's take a closer look at that colon because that may look confusing as well. The colon is also used in python for slicing and when its placed on the right side of an index, it specifies everything after that index including the index.
But hang on, False isn't a number yet. Right, python converts False to 0 so as a final result we have "123456789"[0:]
. So this complicated code does nothing for us because it just returns the entire string "123456789"
.
Now we are at index = numbers.index
, which is really index = "123456789".index
. This stores the index method of the string "123456789"
.
This is used in the sorted method.
https://docs.python.org/3/howto/sorting.html#key-functions states "The value of the key parameter should be a function (or other callable) that takes a single argument and returns a key to use for sorting purposes."
This will call "123456789".index(number) for every number in values, and use the returned index to provide the sorting like this:
Let's recap values = ['1', '3', '9', '6', '7']
and "123456789"
"123456789".index('1') # Gives 0 (because the index of '1' in `"123456789"` is 0)
"123456789".index('3') # Gives 2 (because the index of '3' in `"123456789"` is 2)
"123456789".index('9') # Gives 8
"123456789".index('6') # Gives 5
"123456789".index('7') # Gives 6
Now it uses these numbers and sorts them from least to greatest (0,2,5,6,8) and presents the values from values = ['1', '3', '9', '6', '7']
in that order. (0 corresponded to '1' so that goes first, 2 corresponded to '3', 5 corresponded to '6' and so on.)
values = ['1', '3', '9', '6', '7'] # just an example of what comes in
index = "123456789"["8" in values:].index # everything except this line is understandable
print(sorted(values, key=index))
This code used a very complicated way to sort the list of numerical strings. Below is a more clear way of doing it that will work for lists that have numbers that exceed 1-9.
Recommended (works for all integers):
values = ['1', '3', '9', '6', '7'] # just an example of what comes in
print(sorted(values, key=int))
How does it work? The int
function first converts each element in values to an integer as provided as a key
function to sorted
. When python receives numbers to sort it will automatically sort them from least to greatest.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.