简体   繁体   中英

Sorting lists by only using the numbers in the value

let's say i have this list

a = ['20 - A', '200 - A', '30 - B']

and i want to sort it in a way that will sort by the numbers like so:

['20 - A', '30 - B', '200 - A']

i know i can split the values by finding the first space but i can't think of a way to sort it after i do

>>> a = ['20 - A', '200 - A', '30 - B']
>>> sorted(a, key = lambda x: int(x.split()[0]))

>>> ['20 - A', '30 - B', '200 - A']

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