[英]How to sort strings containing numbers and letters?
I have a list in the following form: 我有以下形式的清单:
['AK 40', 'AK 35', 'AK 20', '2012',
'2011', '2010', '2009', '2009',
'2007', '2006', '2006', '2005',
'2004', '2003', '2003', '2002']
Those years all represent birthyears and groupings of ages. 这些年份都代表出生年份和年龄组。 What I need is to sort them in the following form:
我需要按以下形式对其进行排序:
['2012', '2011', '2010', ... , '2003', '2002', 'AK 20', 'AK 35', 'AK 40']
Basically, the years should be descending, from the youngest person to the oldest. 基本上,年龄应该从最小的年龄到最大的年龄递减。 But starting from age twenty they need to be treated differently.
但是从20岁开始,他们需要得到不同的对待。
I already tried some lambda
functions I found here on stackoverflow, but unfortunately none of them worked for me due to the fact that my list contains only strings and is not mixed with integers. 我已经尝试了一些在stackoverflow上找到的
lambda
函数,但是不幸的是,由于我的列表仅包含字符串,并且没有与整数混合,因此它们对我都不起作用。
If your strings share the same prefix ( AK
in this case), you can use the following lambda
function: 如果您的字符串共享相同的前缀(在本例中为
AK
),则可以使用以下lambda
函数:
sort_func = lambda e : -int(e) if e.isdigit() else int(''.join(filter(str.isdigit, e)))
sorted_list = sorted(l, key = sort_func)
Where l
is your initial list, for your example, this outputs: 其中
l
是您的初始列表,例如,输出:
['2012', '2011', '2010', '2009', '2009', '2007', '2006', '2006', '2005', '2004', '2003', '2003', '2002', 'AK 20', 'AK35', 'AK 40']
The above functions sorts first by the year strings (the -
in -len(e)
is used to sort them in descending order), then, it sorts the age groups in ascending order by the number after the prefix AK
(by filtering out anything that's not a digit from the age groups' strings). 上述功能第一排序在今年字符串(的
-
在-len(e)
是用于他们降序排序),那么,它由前缀后的数字按升序排序年龄组AK
(通过过滤掉任何东西这不是年龄段字符串中的数字)。
seq = ['AK 40', 'AK 35', 'AK 20', '2012', '2011', '2010', '2009', '2009',
'2007', '2006', '2006', '2005', '2004', '2003', '2003', '2002']
years = [year for year in seq if year.isdigit()]
aks = [ak for ak in seq if not ak.isdigit()]
years.sort()
aks.sort()
result = years[::-1] + aks
print(result)
x = ['AK 40', 'AK 35', 'AK 20', '2012', '2011', '2010', '2009', '2009', '2007', '2006', '2006', '2005', '2004', '2003', '2003', '2002']
简短将是
sorted_list = sorted(x, key = lambda e : int(e.replace('AK ', '-')), reverse=True)
You can use a simple sort key: 您可以使用一个简单的排序键:
d = ['AK 40', 'AK 35', 'AK 20', '2012', '2011', '2010', '2009', '2009', '2007', '2006', '2006', '2005', '2004', '2003', '2003', '2002']
new_d = sorted(d, key=lambda x:[1, -1][x.isdigit()]*int(x.split()[-1]))
Output: 输出:
['2012', '2011', '2010', '2009', '2009', '2007', '2006', '2006', '2005', '2004', '2003', '2003', '2002', 'AK 20', 'AK 35', 'AK 40']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.