当我们使用python给一个由字符串组成的列表排序时,常常会排成这样 [‘10a’, ‘11b’, ‘1c’, ‘20d’, ‘21e’, ‘2f’] 这样的形式 ,然而我们想要 [ ‘1c’,‘2f',  ‘10a’, ‘11b’, ‘20d’, ‘21e’] 这样的形式。

很容易想到的思路是用正则匹配数字,然后据此排序。

以下给出代码:

# -*- coding: utf-8 -*-
import re def sort_key(s):
# 排序关键字匹配
# 匹配开头数字序号
if s:
try:
c = re.findall('^\d+', s)[0]
except:
c = -1
return int(c) def strsort(alist):
alist.sort(key=sort_key)
return alist if __name__ == "__main__":
a = ['0preface', '0toc', '10cpu-sched-multi', '11cpu-dialogue', '12dialogue-vm', '13vm-intro', '14vm-api', '15vm-mechanism', '16vm-segmentation', '17vm-freespace', '18vm-paging', '19vm-tlbs', '1dialogue-threeeasy', '20vm-smalltables', '21vm-beyondphys', '22vm-beyondphys-policy', '23vm-vax', '24vm-dialogue', '2intro', '3dialogue-virtualization', '4cpu-intro', '5cpu-api', '6cpu-mechanisms', '7cpu-sched', '8cpu-sched-mlfq', '9cpu-sched-lottery', '25dialogue-concurrency', '26threads-intro', '27threads-api', '28threads-locks', '29threads-locks-usage',
'30threads-cv', '31threads-sema', '32threads-bugs', '33threads-events', '34threads-dialogue', '35dialogue-persistence', '36file-devices', '37file-disks', '38file-raid', '39file-intro', '40file-implementation', '41file-ffs', '42file-journaling', '43file-lfs', '44file-integrity', '45file-dialogue', '46dialogue-distribution', '47dist-intro', '48dist-nfs', '49dist-afs', '50dist-dialogue', 'dialogue-labs', 'dialogue-monitors', 'dialogue-vmm', 'file-ssd', 'lab-projects-systems', 'lab-projects-xv6', 'lab-tutorial', 'threads-monitors', 'vmm-intro'] print(strsort(a))

输出结果:

['dialogue-labs', 'dialogue-monitors', 'dialogue-vmm', 'file-ssd', 'lab-projects-systems', 'lab-projects-xv6', 'lab-tutorial', 'threads-monitors', 'vmm-intro', '0preface', '0toc', '1dialogue-threeeasy', '2intro', '3dialogue-virtualization', '4cpu-intro', '5cpu-api', '6cpu-mechanisms', '7cpu-sched', '8cpu-sched-mlfq', '9cpu-sched-lottery', '10cpu-sched-multi', '11cpu-dialogue', '12dialogue-vm', '13vm-intro', '14vm-api', '15vm-mechanism', '16vm-segmentation', '17vm-freespace', '18vm-paging', '19vm-tlbs', '20vm-smalltables', '21vm-beyondphys', '22vm-beyondphys-policy', '23vm-vax', '24vm-dialogue', '25dialogue-concurrency', '26threads-intro', '27threads-api', '28threads-locks', '29threads-locks-usage', '30threads-cv', '31threads-sema', '32threads-bugs', '33threads-events', '34threads-dialogue', '35dialogue-persistence', '36file-devices', '37file-disks', '38file-raid', '39file-intro', '40file-implementation','41file-ffs', '42file-journaling', '43file-lfs', '44file-integrity', '45file-dialogue', '46dialogue-distribution', '47dist-intro', '48dist-nfs', '49dist-afs', '50dist-dialogue']

 
 
 

python 根据字符串内数字排序的更多相关文章

  1. Python 基础-> 字符串,数字,变量

    Python 基础:字符串,数字,变量 1. 字符串 (信息的一种表达方式) a. 使用引号创建字符串 b. 单引号,双引号,三引号: ', ", ''', ""&quo ...

  2. SQL SERVER 字符串按数字排序

    需求是这样的: 数据库表里面有一个字段类型是nvachar,存的值是数字和字符混合的,要实现先按数字排序,再按字母倒序. 思路: 考虑这个字段的值是否是有规律可循的,把要按数字排序的部分转换为数字,再 ...

  3. python 给三个数字排序,不用sort函数

    # 给三个数字排序# 方法一def sort_d(a,b,c): if a>b: a,b=b,a # print (a,b) if b>c: b,c=c,b if a>b: a,b= ...

  4. Python的字符串与数字

    Python3.0通过“input”实现读取控制台的输入与用户实现交互.值得注意的是input接受的所有数据都是字符串,即使输入的是数字,依然会被当作字符串来处理.这就会出现一些问题,所以需要进行类型 ...

  5. (后端)SQL SERVER 字符串按数字排序

    应用于B1-1,B1-2,B10-1,B11-1 sqlserver肯定不能按照字符串进行排序,需要进行处理一番: select CONVERT(varchar, LEFT(code,1)),conv ...

  6. python中字符串内置方法

    字符串类型 作用:定义姓名.性别等 定义方式: s='lzs' #\n换行 \t缩进4个空格 \r回退上一个打印结果,覆盖上一个打印结果 加上一个\让后面的\变得无意义 内置方法: (优先掌握) 1. ...

  7. mysql 字符串类型数字排序

    排序字段+0,类似  Java 把 其他类型转换成字符串 比如 +“”: SELECT    b.tag_value AS NAME,    sum(b.uv) ASVALUE FROM    met ...

  8. python从字符串内取两个符号之间的内容

    #取字符串中两个符号之间的东东 def txt_wrap_by(self,start_str, end, html): start = html.find(start_str) if start &g ...

  9. MySQL字符串中数字排序的问题

    1.select * from table where 1   order by id*1 desc; 2.select * from table where 1 order by id+0 desc ...

随机推荐

  1. Batch Close process

    @echo offecho.rem Kill all chrome drivertaskkill /im chromedriver.exe /f pause

  2. Linux和Windows启动后台程序

    平时很多时候,我们需要通过脚本命令调用执行程序,集成一体后方便使用快捷.但是启动脚本窗口比较碍眼,能设置为后台运行既方便又美观. Linux启动后台程序 1.后台执行 nohup方法:不挂断的运行命令 ...

  3. 第一课了解SQL

    1.1 数据库基础 其实一直在使用这数据库,当你在邮箱中查询名字时,当在网站上进行搜索,在自动取款机上使用ATM卡,这些操作都是在使用数据库 1.1.1 数据库   数据库:保存有组织的数据的容器(通 ...

  4. js运算符逻辑!和instanceof的优先级

    写js时间长了,运算符优先级很可能自然而然的就形成习惯了,也不需要特别注意优先级的问题. 至少到目前为止,我也没有真正了解过js当中所有运算符的具体优先级.也没有出过什么重大的问题. 但是直到今天,在 ...

  5. leetcode139

    class Solution { public: bool wordBreak(string s, vector<string> wordDict) { vector<, false ...

  6. Ado.net之连接数据库

    一.Ado.net的核心 Ado.net的核心为两组重要的类,一个负责处理软件内部的实际数据(DataSet),另一个负责与外部数据系统通信(DataProvider) DataSet:包含一个或多个 ...

  7. route命令详解

    1.   使用背景:需要接入两个网络,一个是部署环境所在内网环境,这个环境是上不了外网, 外网环境很可能是一个无线网络.如果两者都连接上,很可能导致有一方不能起作用,即外网或内网上不了,常常需要使用繁 ...

  8. redis make编译失败的原因

    make clean redis编译失败可能是: 1.未安装gcc,gcc-c++ yum install gcc yum install gcc-c++ 2.未安装tcl yum install t ...

  9. py2与py3区别总结

    1. py2中的str是py3中的bytes py2中的Unicode是py3中的str 声明一个字符串变量时,py2 和py3都是str类型,但py2代表字节类型,py3代表文本类型 隐式转换: p ...

  10. Object.assign()解释整理

    链接:https://blog.csdn.net/wang252949/article/details/79106160 语法 Object.assign(target, ...sources) 参数 ...