[英]Sort list of string in Python
I need your help to correctly sort the following list of strings.我需要您的帮助才能正确排序以下字符串列表。
I have the next list of strings:我有下一个字符串列表:
listElements = ['2018_1', '2018_10', '2018_2', '2018_3', '2019_1', '2019_10', '2019_2, '2019_3', '2020_1', '2020_10', '2020_2', '2020_3']
And this is the result that I need:这是我需要的结果:
listElements = ['2018_1', '2018_2', '2018_3', '2018_10', '2019_1', '2019_2, '2019_3', '2019_10', '2020_1', 2020_2', '2020_3', 2020_10']
Thanks in advance!提前致谢!
You will have to use sort()
with custom compare function.您必须将
sort()
与自定义比较 function 一起使用。 This function compares first part of string (before _
), then if it is the same, compares second part of string.此 function 比较字符串的第一部分(在
_
之前),然后如果相同,则比较字符串的第二部分。
def comp(item1, item2):
i1 = [int(i) for i in item1.split('_')]
i2 = [int(i) for i in item2.split('_')]
if i1[0] < i2[0]:
return -1
if i1[0] > i2[0]:
return 1
if i1[1] < i2[1]:
return -1
if i1[1] > i2[1]:
return 1
return 0
Here is the driver code:下面是驱动代码:
import functools
listElements = ['2018_1', '2018_10', '2018_2', '2018_3', '2019_1', '2019_10', '2019_2', '2019_3', '2020_1', '2020_10', '2020_2', '2020_3']
def comp(item1, item2):
i1 = [int(i) for i in item1.split('_')]
i2 = [int(i) for i in item2.split('_')]
if i1[0] < i2[0]:
return -1
if i1[0] > i2[0]:
return 1
if i1[1] < i2[1]:
return -1
if i1[1] > i2[1]:
return 1
return 0
listElements.sort(key=functools.cmp_to_key(comp))
print(listElements)
# ['2018_1', '2018_2', '2018_3', '2018_10', '2019_1', '2019_2, '2019_3', '2019_10', '2020_1', 2020_2, '2020_3', 2020_10']
Solved.解决了。
from natsort import natsorted, ns从 natsort 导入 natsorted,ns
natsorted(listElements, alg=ns.IGNORECASE) natsorted(listElements, alg=ns.IGNORECASE)
Using a custom compare function is the “pythonic” way to solve such sorting problems in general.使用自定义比较 function 通常是解决此类排序问题的“pythonic”方法。
Another way is to convert the list to a temporary list that can be sorted natively, then convert the result back to the desired format (which need not be the original format).另一种方法是将列表转换为可以本地排序的临时列表,然后将结果转换回所需的格式(不必是原始格式)。
For example, the following will do what you want:例如,以下将做你想要的:
tmpList = [[int(n) for n in e.split("_")] for e in istElements]
tmpList.sort()
istElements = ["%d_%d" % tuple(p) for p in tmpList]
If you want, you can do all of that within a single statement, using nested list comprehensions or generators, so you don't need the temporary list:如果您愿意,您可以使用嵌套列表推导或生成器在单个语句中完成所有这些操作,因此您不需要临时列表:
istElements = [
"%d_%d" % tuple(p)
for p in sorted(
[
int(n)
for n in e.split("_")
]
for e in istElements
)
]
I've tried to format the statement across multiple lines so the structure of the list comprehensions becomes clear.我试图跨多行格式化语句,以便列表理解的结构变得清晰。
Code代码
sorted(listElements, key = lambda v: [int(i) for i in v.split('_')])
Result结果
['2018_1', '2018_2', '2018_3', '2018_10', '2019_1', '2019_2', '2019_3', '2019_10', '2020_1', '2020_2', '2020_3', '2020_10']
Explanation解释
Key function converts each string to a list of integers which Python list compare function knows how to compare during sorting.键 function 将每个字符串转换为整数列表,Python 列表比较 function 知道如何在排序期间进行比较。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.