当我们使用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. 学习vue容易忽视的细节

    1.对于自定义标签名(组件名称),Vue.js 不强制要求遵循 W3C 规则 (小写,并且包含一个短杠),尽管遵循这个规则比较好.HTML 特性是不区分大小写的.所以,当使用的不是字符串模板,came ...

  2. 初识rt-thread

    bernard.xiong CEO 熊谱祥 env,提供编译构建环境.图形化系统配置及软件包管理功能 scons 是 RT-Thread 使用的编译构建工具,可以使用 scons 相关命令来编译 RT ...

  3. Java异常学习总结一

    Java中的异常 定义 异常(Exception)就是在程序的运行过程中所发生的不正常的事件,它会中断正在运行的程序. 常见类型举例 所需文件找不到(ClassNotFoundException) 网 ...

  4. Linux背背背(3)

    目录 1.文件操作命令 2.文件夹操作命令 文件操作命令 创建 命令:touch 语法:#touch 文件的名字      文件名可以是一个完整的路径 如果后面的参数文件名指定了路径,则表示在指定的路 ...

  5. 八(第三篇)、主体结构元素——time元素、pubdate属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. [java,2017-05-04] 合并word文档

    import java.io.File; import com.aspose.words.Document; import com.aspose.words.ImportFormatMode; pub ...

  7. 怎样让scrollview滚动到底部?

    - (void)scrollsToBottomAnimated:(BOOL)animated { CGFloat offset = self.tableView.contentSize.height ...

  8. Xeon Phi 《协处理器高性能编程指南》随书代码整理 part 1

    ▶ 第三章,逐步优化了一个二维卷积计算的过程 ● 基准代码 #include <stdio.h> #include <stdlib.h> #include <string ...

  9. 手写注解实现SpringMVC

    参考:https://www.cnblogs.com/Shock-W/p/6617068.html

  10. 2017-11-04 Sa Oct 消参

    2017-11-04 Sa $ P(-3, 0) $ 在圆C $ (x-3)^2 + y^2 = 8^2 $ 内,动圆M与圆相切且过P点,求M点轨迹. 设切点 $ A(a, b) $,圆心 \(M(x ...