基于子列表中的字母数字字符串的自然排序列表?

时间:2014-03-07 18:46:49

标签: python string list sorting natural-sort

像泥一样清澈,是吗?我将从一个例子开始......

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)])

2 个答案:

答案 0 :(得分:1)

使用this answer中的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']]
>>> 

答案 1 :(得分:0)

sort的默认设置是使用内置cmp来比较我认为的元素,但您可以这样做,例如:

my_list.sort(key = lambda x: x[1])

这将指示它使用每个子列表的第一个元素作为比较键

编辑:没有lambdas ......

from operator import itemgetter
my_list.sort(key = itemgetter(1))