简体   繁体   中英

How to sort a list of strings which contain numbers and characters

I have list = ['1-10', '1-9', '1-8', '1-11a', '1-12', '1-11b', '1-11'] that should be sorted this way:

sortedlist = ['1-8', '1-9', '1-10', '1-11', '1-11a', '1-11b', '1-12'].

How can I do that?

I can separate the strings via the split method to only care about what's after the - .

list.sort(key=lambda a: a.split('-')[1])

but then 1-10, 1-11 and so on come before 1-8. And I can't use int() because of the 'a' and 'b'.

Using Regex.

Ex:

import re
l = ['1-10', '1-9', '1-8', '1-11a', '1-12', '1-11b', '1-11']

l.sort(key=lambda a: (int(re.search(r"\-(\d+)[a-z]*", a).group(1)), re.search(r"\-\d+([a-z]*)", a).group(1)))
print(l)

Output:

['1-8', '1-9', '1-10', '1-11', '1-11a', '1-11b', '1-12']

Using a tuple in lambda as key parameter:

import re

lst = ['1-10', '1-9', '1-8', '1-11a', '1-12', '1-11b', '1-11'] 

lst.sort(key=lambda a: (int(re.search(r'\d+', a.split('-')[1]).group()), len(a)))

print(lst)
# ['1-8', '1-9', '1-10', '1-11', '1-11a', '1-11b', '1-12']

Using re module:

import re

l = ['1-10', '1-9', '1-8', '1-11a', '1-12', '1-11b', '1-11']

j = [l[i[0]]
    for i in sorted(
            [(i2, *g)
             for i2, i in enumerate(l)
             for g in re.findall(r'(\d+)-(\d+)(\w?)', i)],
        key=lambda k: (int(k[1]), int(k[2]), k[3]))]

print(j)

Prints:

['1-8', '1-9', '1-10', '1-11', '1-11a', '1-11b', '1-12']

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