...and compare it with another list to get the same item in the same index/position.
Eg.
a = [1, 2, 3, 4, 5]
b = [a, b, c, d, e]
I want the highest number in list a so:
5
And then, I compare it with list b to get the item, which is on the same position as the highest number in list a :
e
I apologize for the poorly worded question but this is the best I can explain it.
One-liner: b[a.index(max(a))]
However, this makes two pass through the list, ie first with max(a)
, and second with a.index
.
The snippet below makes only one pass:
max_index, max_value = max(enumerate(a), key=lambda p: p[1])
print(b[max_index])
python has this great built in array function called index
that will take care of what you need.
in your example: x = b[a.index(5)]
which takes a
and gets the index of 5
(which is 4) and then assigns the item in b
at that index to the variable x
so that you can do whatever evaluations you may need.
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.