简体   繁体   English

如何对特定字符串索引中的字符串数组进行排序

[英]How to sort Array of string in specific string index

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)?? Soo.. 在列表中,有名称(其中包含大写字)、代码(名称后的 3 位数字)和密码(前一个代码后的 10 位代码).. 我的问题是,可以我按特定字符串索引对该列表进行排序,我想按代码对其进行排序(名称后的 3 个数字)?

# 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所以我的预期结果是,KIFLA 第一,AELIA 第二,因为 KIFLA 的代码是 500 而 AELIA 的代码是 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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