python 根据字符串内数字排序

当我们使用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']

 
 
 

转载于:https://www.cnblogs.com/chenyansu/p/8300296.html

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值