简体   繁体   中英

sort version nicely with letters and numbers

for some reason I cannot find this example I want to sort version numbers nicely. for example [v2,v10,v1] to [v1,v2,v10] I really only need the max but I can't find anything for that either.

Don't reinvent the .whl , you can use pkg_resources

parse_version(version) Parsed a project's version string as defined by PEP 440 . The returned value will be an object that represents the version. These objects may be compared to each other and sorted.

Demo:

>>> from pkg_resources import parse_version
>>> data = ['v2', 'v10', 'v1', 'v2.1', 'v2.1.1']
>>> sorted(data, key=parse_version)
['v1', 'v2', 'v2.1', 'v2.1.1', 'v10']

You could also pass it as the key to find the latest version:

>>> max(data, key=parse_version)
'v10'

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM