Find A Second Largest Number In A Python List
I was trying to find the second largest number in a list and thought of converting list into set, and to convert it back to list to eliminate the repeated values. Unfortunately, th
Solution 1:
Sets have arbitrary ordering. You can't rely on any particular ordering (it's simply not part of the language spec, so it can change from release to release, or even run to run in theory, and in practice for strings).
Either sort the list
, or use heapq.nlargest
to find the second largest number. In either case, set
is only useful for deduplication, you'll need to convert back to list
to get useful ordering.
Post a Comment for "Find A Second Largest Number In A Python List"