You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Python对包含数字的字符串列表按数字降序排序?

按字符串中的指定数字降序排序的实现方法

问题场景

你有一组包含特定数字的字符串(示例及真实URL列表如下),需要按字符串中目标数字从大到小排序,但默认的字符串排序会按字符顺序比较(比如"4"会排在"21"前面),无法满足需求。

示例输入

l = ["sometext-2022-21_sometext", 
"sometext-2022-4_sometext", 
"sometext-2022-121_sometext",
"sometext-2022-321_sometext", 
"sometext-2022-1_sometext",
"sometext-2022-31_sometext"]

期望输出

l = ["sometext-2022-321_sometext", 
"sometext-2022-121_sometext", 
"sometext-2022-31_sometext",
"sometext-2022-21_sometext", 
"sometext-2022-4_sometext",
"sometext-2022-1_sometext"]

真实URL列表

["https://essgfsgffghe.ch/docs/BS_Omni/BS_APG_001_AUS-2022-401_nodate.html", 
"https://ensfgtsfgscheidsuche.ch/docs/BS_Omni/BS_APG_001_BEZ-2022-39_nodate.html", 
"https://ensfgtsfguche.ch/docs/BS_Omni/BS_APG_001_VD-2022-5_nodate.html", 
"https://egsfsfgtscheidsuche.ch/docs/BS_Omni/BS_APG_001_VD-2022-2_nodate.html", 
"https://ensfgidsuche.ch/docs/BS_Omni/BS_APG_001_BES-2022-83_nodate.html", 
"https://sfgfgnsfgche.ch/docs/BS_Omni/BS_SVG_001_IV-2022-54_nodate.html", 
"https://entscsfghe.ch/docs/BS_Omni/BS_APG_001_BES-2022-36_nodate.html", 
"https://essntscsfguche.ch/docs/BS_Omni/BS_APG_001_AUS-2022-32_nodate.html", 
"https://entsfgeidsuche.ch/docs/BS_Omni/BS_APG_001_BES-2022-89_nodate.html", 
"https://entfsfgsgsfuche.ch/docs/BS_Omni/BS_APG_001_AUS-2022-412_nodate.html", 
"https://ensfgche.ch/docs/BS_Omni/BS_APG_001_VD-2022-5_nodate.html", 
"https://ensfgse.ch/docs/BS_Omni/BS_APG_001_BES-2022-70_nodate.html", 
"https://esfgche.ch/docs/BS_Omni/BS_APG_001_VD-2022-1_nodate.html"]

解决方案

核心思路是给Python的sort()方法传入自定义排序键,提取出字符串中用于排序的数字并转为整数,再按整数大小降序排序。

方法1:字符串分割(格式固定场景适用)

针对你的字符串格式(目标数字在-2022-之后、_之前),直接通过字符串分割提取数字:

# 定义真实URL列表
url_list = ["https://essgfsgffghe.ch/docs/BS_Omni/BS_APG_001_AUS-2022-401_nodate.html", 
"https://ensfgtsfgscheidsuche.ch/docs/BS_Omni/BS_APG_001_BEZ-2022-39_nodate.html", 
"https://ensfgtsfguche.ch/docs/BS_Omni/BS_APG_001_VD-2022-5_nodate.html", 
"https://egsfsfgtscheidsuche.ch/docs/BS_Omni/BS_APG_001_VD-2022-2_nodate.html", 
"https://ensfgidsuche.ch/docs/BS_Omni/BS_APG_001_BES-2022-83_nodate.html", 
"https://sfgfgnsfgche.ch/docs/BS_Omni/BS_SVG_001_IV-2022-54_nodate.html", 
"https://entscsfghe.ch/docs/BS_Omni/BS_APG_001_BES-2022-36_nodate.html", 
"https://essntscsfguche.ch/docs/BS_Omni/BS_APG_001_AUS-2022-32_nodate.html", 
"https://entsfgeidsuche.ch/docs/BS_Omni/BS_APG_001_BES-2022-89_nodate.html", 
"https://entfsfgsgsfuche.ch/docs/BS_Omni/BS_APG_001_AUS-2022-412_nodate.html", 
"https://ensfgche.ch/docs/BS_Omni/BS_APG_001_VD-2022-5_nodate.html", 
"https://ensfgse.ch/docs/BS_Omni/BS_APG_001_BES-2022-70_nodate.html", 
"https://esfgche.ch/docs/BS_Omni/BS_APG_001_VD-2022-1_nodate.html"]

# 自定义排序键函数:提取目标数字
def get_sort_key(s):
    # 按"-2022-"分割字符串,取第二部分
    num_part = s.split("-2022-")[1]
    # 按"_"分割,取第一部分并转为整数
    return int(num_part.split("_")[0])

# 按数字降序排序
url_list.sort(key=get_sort_key, reverse=True)

# 打印排序结果
for item in url_list:
    print(item)

方法2:正则表达式(格式灵活场景适用)

如果字符串格式有变化,但目标数字的特征明确(比如是-2022-后的连续数字),可以用正则表达式匹配提取,通用性更强:

import re

url_list = ["https://essgfsgffghe.ch/docs/BS_Omni/BS_APG_001_AUS-2022-401_nodate.html", 
"https://ensfgtsfgscheidsuche.ch/docs/BS_Omni/BS_APG_001_BEZ-2022-39_nodate.html", 
"https://ensfgtsfguche.ch/docs/BS_Omni/BS_APG_001_VD-2022-5_nodate.html", 
"https://egsfsfgtscheidsuche.ch/docs/BS_Omni/BS_APG_001_VD-2022-2_nodate.html", 
"https://ensfgidsuche.ch/docs/BS_Omni/BS_APG_001_BES-2022-83_nodate.html", 
"https://sfgfgnsfgche.ch/docs/BS_Omni/BS_SVG_001_IV-2022-54_nodate.html", 
"https://entscsfghe.ch/docs/BS_Omni/BS_APG_001_BES-2022-36_nodate.html", 
"https://essntscsfguche.ch/docs/BS_Omni/BS_APG_001_AUS-2022-32_nodate.html", 
"https://entsfgeidsuche.ch/docs/BS_Omni/BS_APG_001_BES-2022-89_nodate.html", 
"https://entfsfgsgsfuche.ch/docs/BS_Omni/BS_APG_001_AUS-2022-412_nodate.html", 
"https://ensfgche.ch/docs/BS_Omni/BS_APG_001_VD-2022-5_nodate.html", 
"https://ensfgse.ch/docs/BS_Omni/BS_APG_001_BES-2022-70_nodate.html", 
"https://esfgche.ch/docs/BS_Omni/BS_APG_001_VD-2022-1_nodate.html"]

# 正则匹配模式:捕获-2022-后的连续数字
pattern = re.compile(r'-2022-(\d+)_')

def get_sort_key_regex(s):
    match_result = pattern.search(s)
    if match_result:
        return int(match_result.group(1))
    return 0  # 无匹配项时默认返回0,排在末尾

# 降序排序
url_list.sort(key=get_sort_key_regex, reverse=True)

# 打印结果
for item in url_list:
    print(item)

排序结果说明

两种方法都会得到按目标数字从大到小排列的列表,顺序为:412 → 401 → 89 → 83 → 70 → 54 → 39 → 36 → 32 → 5 → 5 → 2 → 1。

内容的提问来源于stack exchange,提问作者taga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 09:55:21