Skip to main content
 首页 » 编程设计

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

2025年05月04日20mate10pro

一尘不染,是吗?我将从一个例子开始......

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 的代码,上述案例就是一个“简单”的修复。我将翻译语句添加到他的自然排序函数到下面,基于他如何处理他的关键函数(因为这是我想要的排序方式,它只是每行排序是不正确的),如下所示。

        alphanum_key = (lambda key: 
                        [convert(c.translate(None, punctuation + whitespace)) for c in re.split('([0-9]+)', key)]) 

请您参考如下方法:

使用 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']] 
>>>