![](/img/trans.png)
[英]Python . How to sort elements of sub-list if second elements are equal sort the sub-lists by first element
[英]Natural sort of a list of lists based on an alphanumeric string within sub-list?
像泥一样清澈,是吗? 我将从一个例子开始...
my_list = [[4,'A4, A23, A3, A6', 'Description 1', 'Property 1'],
[4,'B3, B35, B10, B22', 'Description 2', 'Property 2'],
[6,'A1, A11, A10, A21, A2, A22', 'Description 3', 'Property 3']]
应该排序为:
>>>my_list:
[[6,'A1, A2, A10, A11, A21, A22', 'Description 3', 'Property 3']
[4,'A3, A4, A6, A23', 'Description 1', 'Property 1'],
[4,'B3, B10, B22, B35', 'Description 2', 'Property 2']]
因此,我首先需要对每个子列表的第二个索引中的字符串进行自然排序,然后我需要根据子列表的第二个索引中的字符串对所有列表进行自然排序。 我整天都在撞墙,所以我发布任何尝试的代码都可能会导致错误。
也许我最初的示例案例不够稳健,但这是我基于@Ashwini的代码得到的结果:
[[ 1, 'C1', 'DW-00232'],
[3, 'C11, C32, C46', 'DW-6546'],
[7, 'C16, C17, C18, C19, C20, C21, C25', 'DW-545'],
[5, 'C2, C3, C4, C5, C63', 'DW-7657'],
[1, 'C26', 'DW-0056'],
[2, 'C59, C60', 'DW-23424'],
[5, 'C6, C13, C24, C30, C64', 'DW-5345']]
我希望输出如下:
[[ 1, 'C1', 'DW-00232'],
[5, 'C2, C3, C4, C5, C63', 'DW-7657'],
[5, 'C6, C13, C24, C30, C64', 'DW-5345'],
[3, 'C11, C32, C46', 'DW-6546'],
[7, 'C16, C17, C18, C19, C20, C21, C25', 'DW-545'],
[1, 'C26', 'DW-0056'],
[2, 'C59, C60', 'DW-23424']]
球门柱不断移动。 现在,我需要考虑某些字母数字组合在括号中的情况。 我需要在排序过程中忽略括号。
例:
[[ 1, 'C1', 'DW-00232'],
[ 7, '(C21), C16, (C7), (C18), C19, C6, C65', 'DW-545'],
[ 5, ' C4, (C2), C3, C10, (C5)', 'DW-7657']]
排序为:
[[ 1, 'C1', 'DW-00232'],
[ 5, '(C2), C3, C4, (C5), C10', 'DW-7657'],
[ 7, 'C6, (C7), C16, (C18), C19, (C21), C65', 'DW-545']]
好的,一旦我仔细检查了Ashwini的代码,上述情况就是一个“简单”的解决方案。 我根据他对键函数的处理方式(以下是我想要的方式对它进行排序,因此仅将每行排序不正确)添加了translate语句到他的自然排序函数中,如下所示。
alphanum_key = (lambda key:
[convert(c.translate(None, punctuation + whitespace)) for c in re.split('([0-9]+)', key)])
使用此答案中的natural_sort
函数,您可以执行以下操作:
import re
from string import punctuation as punc, whitespace as wt
from pprint import pprint
def natural_sort(l):
#https://stackoverflow.com/a/4836734/846892
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
return sorted(l, key = alphanum_key)
def key(seq):
convert = lambda text: int(text) if text.isdigit() else text.lower()
return [convert(c.translate(None, punc+wt)) for c in re.split('([0-9]+)', seq)]
...
>>> my_list = [[ 1, 'C1', 'DW-00232'],
[3, 'C11, C32, C46', 'DW-6546'],
[7, 'C16, C17, C18, C19, C20, C21, C25', 'DW-545'],
[5, 'C2, C3, C4, C5, C63', 'DW-7657'],
[1, 'C26', 'DW-0056'],
[2, 'C59, C60', 'DW-23424'],
[5, 'C6, C13, C24, C30, C64', 'DW-5345']]
>>>
>>> new_lis = [x[:1] + [", ".join(natural_sort(x[1].split(', ')))] + x[2:]
for x in my_list]
>>> new_lis.sort(key = lambda x:key(x[1]))
>>> pprint(new_lis)
[[1, 'C1', 'DW-00232'],
[5, 'C2, C3, C4, C5, C63', 'DW-7657'],
[5, 'C6, C13, C24, C30, C64', 'DW-5345'],
[3, 'C11, C32, C46', 'DW-6546'],
[7, 'C16, C17, C18, C19, C20, C21, C25', 'DW-545'],
[1, 'C26', 'DW-0056'],
[2, 'C59, C60', 'DW-23424']]
>>>
默认的sort
是使用内置的cmp
比较我认为的元素,但是您可以这样做,例如:
my_list.sort(key = lambda x: x[1])
这将指示它使用每个子列表的第一个元素作为比较键
编辑:没有lambdas ...
from operator import itemgetter
my_list.sort(key = itemgetter(1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.