简体   繁体   English

用数字对Python字符串列表进行排序

[英]Sort a python list of strings with a numeric number

I have a list of filenames called filelist 我有一个名为filelist的文件名列表

 In []: filelist
Out []: ['C:\\Mon20412\\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
         'C:\\Mon25312\\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx',
         'C:\\Mon20362\\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx']

I want to sort this filelist by the numerical value that is in the bolded position 我想按粗体位置的数值对文件列表进行排序

C:\\Mon20412\\P-2NODE-RAID6- 1 BLACK-32k-100-segmented.xlsx C:\\ Mon20412 \\ P-2NODE-RAID6-1 1 BLACK-32k-100-segmented.xlsx
C:\\Mon25312\\P-2NODE-RAID6- 13 RED-32k-100-segmented.xlsx C:\\ Mon25312 \\ P-2NODE-RAID6-1 13 RED-32k-100-segmented.xlsx
C:\\Mon20362\\P-2NODE-RAID6- 2 GREEN-32k-100-segmented.xlsx C:\\ Mon20362 \\ P-2NODE-RAID6- 2 GREEN-32k-100-segmented.xlsx

So in this example, the output would be 因此,在此示例中,输出为

Out []: ['C:\\Mon20412\\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
         'C:\\Mon20362\\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx'
         'C:\\Mon25312\\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx']

Thank you! 谢谢!

import re

f = lambda s: int(re.findall(r'.*RAID6-(\d+).*', s)[0])
sorted(l, key=f)

Find a good, reliable way to extract the number that you want. 找到一种好的可靠方法来提取所需的号码。 Then sort by that number, using the key argument. 然后使用key参数按该数字排序。 This seems to be reliable enough for your input, but it's not efficient. 对于您的输入,这似乎足够可靠,但是效率不高。

a = ['C:\\Mon20412\\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
    'C:\\Mon25312\\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx',
    'C:\\Mon20362\\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx']

def k(a):
    x = a.split("\\")[-1].split("-")[3]
    y = filter(lambda x: x in "0123456789", x)
    return int("".join(list(y)))


print(sorted(a, key=k))

output: 输出:

['C:\\Mon20412\\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx', 
'C:\\Mon20362\\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx',
'C:\\Mon25312\\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx']

Use a regex to parse out the number and use that as a sort key. 使用正则表达式解析数字并将其用作排序键。

Quick and dirty: 快速又肮脏:

import re

l = ['C:\\Mon20412\\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
     'C:\\Mon25312\\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx',
     'C:\\Mon20362\\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx']

def get_sort_number(s):
    pattern = r'C:\\Mon\d+\\P-2NODE-RAID6-(\d+)'

    try:
        return int(re.match(pattern, s).group(1))
    except AttributeError:
        return 0

sorted(l, key=get_sort_number)

This gives 这给

['C:\\Mon20412\\P-2NODE-RAID6-1BLACK-32k-100-segmented.xlsx',
 'C:\\Mon20362\\P-2NODE-RAID6-2GREEN-32k-100-segmented.xlsx',
 'C:\\Mon25312\\P-2NODE-RAID6-13RED-32k-100-segmented.xlsx']

All strings that cannot be matched by the regex would be at the beginning of the sorted list. 正则表达式无法匹配的所有字符串都将在排序列表的开头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - 排序字符串/数字 - Python - sort strings / numeric Python 按最后三个字符和一个数字对字符串列表中的字符串进行排序 - Python sort strings in a list of strings by 3 last characters and a number 在Python中,我如何自然地对字母数字字符串列表进行排序,以使字母字符排在数字字符之前? - In Python, how can I naturally sort a list of alphanumeric strings such that alpha characters sort ahead of numeric characters? 如何使用数字和字母字符串对字典列表进行排序? - How to sort a list of dicts with numeric and alphabetic strings? 如何在Python中对带有结尾零的数字字符串进行排序? - How to sort numeric strings with trailing zeroes in Python? python排序字​​符串列表使用正则表达式与不同数量的模式匹配 - python sort list of strings using regex with varying number of pattern matches Python:如果列表同时包含数字和字符串,如何排序? - Python : How to sort if the list contains the number and strings at the same time? 如何对python字符串列表进行排序,字符串末尾包含数字 - How to sort a python list of strings which contain a number in the end 在python中按数字对字符串列表进行排序 - Sort list of Strings by numbers in python Python-如何对字符串列表进行排序 - Python - How to sort a list of strings
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM