I'd like to know how i can find the two longest strings from a list(array) of strings or how to find the second longest string from a list. thanks
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You can do this using the standard heapq module:
If a is your list of strings, then a.sort(key=len) will sort your list of strings by their length. The longest would be a[-1], and the second longest would be a[-2]. `
Easiest way to do this is use the
sorted()
function by using another built-in functionlen
as thekey
argument as follows;or if you need the two longest: