So.. Actually, I have a list that has multiple strings on it. And here I want to sort it in specific strings index:
lists = ['AELIA4560005253120', 'KIFLA5000005760000']
Soo.. inside of the lists, there are names (which has caps word in it), code (3 number digits after the name), and secret number (10 digits of code after the previous code).. my question is, can I sort this list by specific strings index which I want to sort it by code (3 number digits after the name)??
# expecting lists after sort
lists_sort = ['KIFLA5000005760000','AELIA4560005253120']
So my expected result is, KIFLA came first and AELIA is second is because KIFLA's codes are 500 and AELIA's codes are 456
Can anyone help me? thank you
Try this:
lists = ['AELIA4560005253120', 'KIFLA5000005760000']
res = sorted(lists, key=lambda x: int(x[5:8]) , reverse = True)
#['KIFLA5000005760000', 'AELIA4560005253120']
Assuming the name is of a variable length, you can use this:
import re
lists = ['AELIA4560005253120', 'KIFLA5000005760000']
lists_sort = sorted(lists, key = lambda x: int(re.search(r'\d{3}', x).group(0)), reverse = True)
print(lists_sort)
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.