3

我想对每个字符串列表进行排序,例如:

list1 = ['3DT1_PN_DIS3D_S001', '3DT1_PN_noDIS3D_S001', '3DT1_S001', '3DT1_noPN_DIS3D_S001']
list2 = ['3DT1_noPN_DIS3D_S002', '3DT1_PN_noDIS3D_S002', '3DT1_PN_DIS3D_S002']

跟随模式[ '3DT1_S##', '3DT1_noPN_DIS3D_S##', '3DT1_PN_noDIS3D_S##', '3DT1_PN_DIS3D_S##']

结果应该是:

list1 = [ '3DT1_S001', '3DT1_noPN_DIS3D_S001', '3DT1_PN_noDIS3D_S001', '3DT1_PN_DIS3D_S001']
list2 = [ '3DT1_noPN_DIS3D_S002', '3DT1_PN_noDIS3D_S002', '3DT1_PN_DIS3D_S002']

我尝试使用 sorted 方法,但没有运气!

有什么帮助吗?

4

4 回答 4

4

您可以定义一个按所需顺序返回元组的键函数,然后将该函数传递给like so的key参数。sorted

>>> def key_fn(x):
...     tags = x.split('_')
...     if tags[1][0] == 'S':
...         return (0, int(tags[1][1:]))
...     elif tags[1] == 'noPN':
...         return (1, int(tags[3][1:]))
...     elif tags[1] == 'PN':
...         if tags[2] == 'noDIS3D':
...             return (2, int(tags[3][1:]))
...         else:
...             return (3, int(tags[3][1:]))
... 
>>> list1 = ['3DT1_PN_DIS3D_S001', '3DT1_PN_noDIS3D_S001', '3DT1_S001', '3DT1_noPN_DIS3D_S001']
>>> sorted(list1, key=key_fn)
['3DT1_S001', '3DT1_noPN_DIS3D_S001', '3DT1_PN_noDIS3D_S001', '3DT1_PN_DIS3D_S001']
于 2012-04-13T15:22:59.740 回答
3

我的两分钱......这有一个定义订单的'patternList'变量。这可能是实现这一点的最简单(最易读、可扩展)的方法:没有凌乱的 if-else。此外,具有相同起始模式的列表项按字符串的其余部分排序。

list1.sort(key = myKey)意味着对于每个列表项myKey功能在排序之前执行。函数仅以正常排序将执行您想要的方式myKey修改已排序的列表项以进行排序。在输出排序列表中,未使用原始列表项(不是修改的项)。myKey

在下面的示例中,myKey 函数将列表项分成两部分,并根据 patternList 变量用整数标记第一部分。普通排序可以以您想要的方式处理返回的元组。

list1 = ['3DT1_PN_DIS3D_S001', '3DT1_PN_noDIS3D_S001', '3DT1_S001', '3DT1_noPN_DIS3D_S001']
list2 = ['3DT1_noPN_DIS3D_S002', '3DT1_PN_noDIS3D_S002', '3DT1_PN_DIS3D_S002', '3DT1_PN_DIS3D_S003', '3DT1_PN_DIS3D_S001']

def myKey(x):
    # create the 'order list' for starting pattern
    patternsList = [ '3DT1_S', '3DT1_noPN_DIS3D_S', '3DT1_PN_noDIS3D_S', '3DT1_PN_DIS3D_S']
    for i in range(len(patternsList)): # iterate patterns in order
        pattern = patternsList[i]
        if x.find(pattern) == 0: # check if x starts with pattern
            # return order value i and x without the pattern
            return (i, x.replace(pattern, '')) 

    # if undefined pattern is found, put it to first
    return (-1, x)

    # alternatively if you want undefind to be last
    # return (len(patternList)+1, x)


print list1
list1.sort(key = myKey)
print list1

print list2
list2.sort(key = myKey)
print list2
于 2012-04-14T22:19:14.020 回答
1

此方法通过按找到的第一个模式的索引进行排序来工作。

>>> import re
>>> list1 = ['3DT1_PN_DIS3D_S001', '3DT1_PN_noDIS3D_S001', '3DT1_S001', '3DT1_noPN_DIS3D_S001']
>>> list2 = ['3DT1_noPN_DIS3D_S002', '3DT1_PN_noDIS3D_S002', '3DT1_PN_DIS3D_S002']
>>> patterns = [ '3DT1_S', '3DT1_noPN_DIS3D_S', '3DT1_PN_noDIS3D_S', '3DT1_PN_DIS3D_S']
>>> pattern = '|'.join('(%s)'%x for x in patterns)
>>> pattern #Creates a regex pattern with each pattern as a group in order
'(3DT1_S)|(3DT1_noPN_DIS3D_S)|(3DT1_PN_noDIS3D_S)|(3DT1_PN_DIS3D_S)'
>>> def sort_key(x):
        return re.match(pattern,x).lastindex
>>> list1, list2 = [sorted(l, key=sort_key) for l in (list1,list2)]
>>> list1
['3DT1_S001', '3DT1_noPN_DIS3D_S001', '3DT1_PN_noDIS3D_S001', '3DT1_PN_DIS3D_S001']
>>> list2
['3DT1_noPN_DIS3D_S002', '3DT1_PN_noDIS3D_S002', '3DT1_PN_DIS3D_S002']
于 2012-04-13T15:31:10.393 回答
-1

这是一种采用“前缀”列表的方法,该列表用于在排序之前对列表进行分组。每个项目都添加到与其匹配的第一个前缀对应的组中,并且只有第一个前缀。

list1 = ['3DT1_PN_DIS3D_S001', '3DT1_PN_noDIS3D_S001', '3DT1_S001', '3DT1_noPN_DIS3D_S001']
list2 = ['3DT1_noPN_DIS3D_S002', '3DT1_PN_noDIS3D_S002', '3DT1_PN_DIS3D_S002', '3DT1_S002']

prefixes = [ '3DT1_S', '3DT1_noPN_DIS3D_S', '3DT1_PN_noDIS3D_S', '3DT1_PN_DIS3D_S']

def f(l):
    result = []
    for p in prefixes:               # for each prefix, in order
        a = []                       # items in the group
        b = []                       # items not in the group
        for x in l:                  # for each item
            if x.startswith(p):      # does the item match the prefix?
                a.append(x)          # add it to the group
            else:  
                b.append(x)          # add it to the "rest"
        result.append(sorted(a))     # sort the group and save it for the result
        l = b                        # continue with the non-group elements
    return result

结果如下:

>>> f(list1)
[['3DT1_S001'], ['3DT1_noPN_DIS3D_S001'], ['3DT1_PN_noDIS3D_S001'], ['3DT1_PN_DIS3D_S001']]
>>> f(list2)
[['3DT1_S002'], ['3DT1_noPN_DIS3D_S002'], ['3DT1_PN_noDIS3D_S002'], ['3DT1_PN_DIS3D_S002']]
于 2012-04-13T15:25:01.723 回答