zoukankan      html  css  js  c++  java
  • 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']

     
     
     
  • 相关阅读:
    Java 设计模式——状态模式
    Java 设计模式——外观模式
    Java高级之虚拟机加载机制
    17.1.1.6 Creating a Data Snapshot Using Raw Data Files 创建一个数据快照使用 Raw Data Files
    17.1.1.5 Creating a Data Snapshot Using mysqldump
    17.1.1.4 Obtaining the Replication Master Binary Log Coordinates 得到复制master binary log 位置:
    17.1.1.3 Creating a User for Replication 创建一个用于用于复制:
    17.1.1.2 Setting the Replication Slave Configuration
    17.1.1.1 Setting the Replication Master Configuration 设置复制的master 配置:
    17.1.1 How to Set Up Replication 设置复制:
  • 原文地址:https://www.cnblogs.com/chenyansu/p/8300296.html
Copyright © 2011-2022 走看看