Python实现对特定列表进行从小到大排序操作示例

本文实例讲述了Python实现对特定列表进行从小到大排序操作。分享给大家供大家参考,具体如下:

1、在系统内新建文件rizhireplacelist.txt

root@kali:~# cd python/
root@kali:~/python# ls
111.txt           listsalaryver2.py  readfile2.py            rizhireplacelist.txt  rizhi.txt            tixingexcel.txt     tixingsort.txt
contact_list.txt  listshop.py        replacefile1.py         rizhireplace.txt      shoplist.txt         tixinglistsort.py   tixing.txt
listbuy.py        manageserach.py    rizhireplaceexcel.txt   rizhisort.py          test.py              tixinglistsort.txt
listsalary.py     readfile1.py       rizhireplacefenhao.txt  rizhitestsort.py      tixingexcelsort.txt  tixinglist.txt
root@kali:~/python#cat rizhireplacelist.txt
['2010530181', '2010530143', '2010411116', '2010156673', '2001180073', '2001180072', '2001180071', '2001180069', '2001180068', '2001180066', '2001180065', '2001180064', '2001180059', '2001180053', '2001180051', '2001180049', '2001180048', '2001180047', '2001180046', '2001180043', '2001180042', '2001180041', '2001180040', '2001180039', '2001180038', '2001180037', '2001180036', '2001180035', '2001180034', '2001180033', '2020539277', '2020539221', '2020535288', '2020260682', '2010620102', '2010570085', '2010570070', '2010500395', '2010470053', '2001610026', '2001610025', '2001180067', '2001180045', '2001180044', '2001180001', '2001020088', '2001000583', '2001000241', '2000830359', '2000632422', '2000016602', '2000015599', '2000011716', '2001180032', '2001180031', '2001180030', '2001180029', '2001180028', '2001180027', '2001180026', '2001180025', '2001180024', '2001180023', '2001180022', '2001180021', '2001180020', '2001180019', '2001180018', '2001180017', '2001180016', '2001180015', '2001180014', '2001180013', '2001180012', '2001180011', '2001180010', '2001180009', '2001180008', '2001180007', '2001180006', '2001180005', '2001180004', '2001180003', '2001180002', '2001180000', '1000019942', '1000018830', '1000018824', '4000230040', '4000219918', '2020571702', '2020260278', '2010540076', '2010540073', '2010540068', '2010505025', '2010505024', '2010500195', '2010500191', '2010500190', '2010500189', '2010500047', '2010500046', '2010419836', '2010310986', '2010310985', '2001240027', '2001180058', '2001180057', '2000831570', '2000771823', '2000771820', '2000771677', '2000771147', '2000771116', '2000771112', '2000631255', '2000021854', '1000019921', '1000018884', '1000018875', '1000018869', '1000018842', '1000017774', '2060210271', '2060210251', '2001180052', '2001180050', '2000632723', '2001180063', '2001180061', '2001180060', '2001180056', '2001180055', '2001180054', '100000000094646', '10000000003629', '10000000002412', '10000000002328', '10000000001057', '100000000094709', '100000000094680', '100000000073254', '10000000003949', '10000000003947', '10000000003556', '10000000003554', '10000000002167', '10000000002066', '10000000001096', '10000000000786', '10000000000782', '10000000000594']

2、编写脚本代码

root@kali:~/python# cat rizhitestsort.py
#!/usr/bin/python
#--*-- coding:utf-8 --*--
import re
befersort = []#
midchang = []
aftersort= []
f1 = file('rizhireplacelist.txt')#打开文件rizhireplacelist.txt
for p in f1.readlines():#逐行读取rizhireplacelist.txt文件
#  print p
  befersort = p#把逐行读取的内容存放到befersort
  print befersort
print '------------------------------------------------------'
count = len(befersort)#计算列表长度
print count
close.f1()
mode = re.compile(r'\d+')#\d是匹配数字字符[0-9],+匹配一个或多个,放在一起是匹配一个或多个数字字符,比如:'1‘、'34‘、'9999‘
midchang = mode.findall(befersort)#对befersort列表进行正则匹配,并存储到midchang
#print midchang
aftersort = [int(x) for x in midchang]#读取被正则匹配成功的每个数字,并存储到aftersort中
aftersort.sort()#对aftersort进行正序排列
print aftersort
root@kali:~/python#

3、实操脚本运行

root@kali:~/python# python rizhitestsort.py
['2010530181', '2010530143', '2010411116', '2010156673', '2001180073', '2001180072', '2001180071', '2001180069', '2001180068', '2001180066', '2001180065', '2001180064', '2001180059', '2001180053', '2001180051', '2001180049', '2001180048', '2001180047', '2001180046', '2001180043', '2001180042', '2001180041', '2001180040', '2001180039', '2001180038', '2001180037', '2001180036', '2001180035', '2001180034', '2001180033', '2020539277', '2020539221', '2020535288', '2020260682', '2010620102', '2010570085', '2010570070', '2010500395', '2010470053', '2001610026', '2001610025', '2001180067', '2001180045', '2001180044', '2001180001', '2001020088', '2001000583', '2001000241', '2000830359', '2000632422', '2000016602', '2000015599', '2000011716', '2001180032', '2001180031', '2001180030', '2001180029', '2001180028', '2001180027', '2001180026', '2001180025', '2001180024', '2001180023', '2001180022', '2001180021', '2001180020', '2001180019', '2001180018', '2001180017', '2001180016', '2001180015', '2001180014', '2001180013', '2001180012', '2001180011', '2001180010', '2001180009', '2001180008', '2001180007', '2001180006', '2001180005', '2001180004', '2001180003', '2001180002', '2001180000', '1000019942', '1000018830', '1000018824', '4000230040', '4000219918', '2020571702', '2020260278', '2010540076', '2010540073', '2010540068', '2010505025', '2010505024', '2010500195', '2010500191', '2010500190', '2010500189', '2010500047', '2010500046', '2010419836', '2010310986', '2010310985', '2001240027', '2001180058', '2001180057', '2000831570', '2000771823', '2000771820', '2000771677', '2000771147', '2000771116', '2000771112', '2000631255', '2000021854', '1000019921', '1000018884', '1000018875', '1000018869', '1000018842', '1000017774', '2060210271', '2060210251', '2001180052', '2001180050', '2000632723', '2001180063', '2001180061', '2001180060', '2001180056', '2001180055', '2001180054', '100000000094646', '10000000003629', '10000000002412', '10000000002328', '10000000001057', '100000000094709', '100000000094680', '100000000073254', '10000000003949', '10000000003947', '10000000003556', '10000000003554', '10000000002167', '10000000002066', '10000000001096', '10000000000786', '10000000000782', '10000000000594']
------------------------------------------------------
2218
[1000017774, 1000018824, 1000018830, 1000018842, 1000018869, 1000018875, 1000018884, 1000019921, 1000019942, 2000011716, 2000015599, 2000016602, 2000021854, 2000631255, 2000632422, 2000632723, 2000771112, 2000771116, 2000771147, 2000771677, 2000771820, 2000771823, 2000830359, 2000831570, 2001000241, 2001000583, 2001020088, 2001180000, 2001180001, 2001180002, 2001180003, 2001180004, 2001180005, 2001180006, 2001180007, 2001180008, 2001180009, 2001180010, 2001180011, 2001180012, 2001180013, 2001180014, 2001180015, 2001180016, 2001180017, 2001180018, 2001180019, 2001180020, 2001180021, 2001180022, 2001180023, 2001180024, 2001180025, 2001180026, 2001180027, 2001180028, 2001180029, 2001180030, 2001180031, 2001180032, 2001180033, 2001180034, 2001180035, 2001180036, 2001180037, 2001180038, 2001180039, 2001180040, 2001180041, 2001180042, 2001180043, 2001180044, 2001180045, 2001180046, 2001180047, 2001180048, 2001180049, 2001180050, 2001180051, 2001180052, 2001180053, 2001180054, 2001180055, 2001180056, 2001180057, 2001180058, 2001180059, 2001180060, 2001180061, 2001180063, 2001180064, 2001180065, 2001180066, 2001180067, 2001180068, 2001180069, 2001180071, 2001180072, 2001180073, 2001240027, 2001610025, 2001610026, 2010156673, 2010310985, 2010310986, 2010411116, 2010419836, 2010470053, 2010500046, 2010500047, 2010500189, 2010500190, 2010500191, 2010500195, 2010500395, 2010505024, 2010505025, 2010530143, 2010530181, 2010540068, 2010540073, 2010540076, 2010570070, 2010570085, 2010620102, 2020260278, 2020260682, 2020535288, 2020539221, 2020539277, 2020571702, 2060210251, 2060210271, 4000219918L, 4000230040L, 10000000000594L, 10000000000782L, 10000000000786L, 10000000001057L, 10000000001096L, 10000000002066L, 10000000002167L, 10000000002328L, 10000000002412L, 10000000003554L, 10000000003556L, 10000000003629L, 10000000003947L, 10000000003949L, 100000000073254L, 100000000094646L, 100000000094680L, 100000000094709L]

PS:这里再为大家推荐一款关于排序的演示工具供大家参考:

在线动画演示插入/选择/冒泡/归并/希尔/快速排序算法过程工具:
http://tools.jb51.net/aideddesign/paixu_ys

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python列表(list)操作技巧总结》、《Python编码操作技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

(0)

相关推荐

  • python实现列表的排序方法分享

    这次代码主要是实现列表的排序,使用sort函数实现,sort函数是对列表中的元素按照特定顺序进行排序,默认reverse,为false,从小到大排序, 如果指定reverse=True,从大到小. 语法格式:l.sort(),希望对你们有所帮助 第一步:先打开我们的pycharm软件,然后新建一个python文件."file","new","python file" 第二步:新建好我们的python文件之后,在我们的编辑页面输入以下代码: l=[

  • Python常见排序操作示例【字典、列表、指定元素等】

    本文实例讲述了Python常见排序操作.分享给大家供大家参考,具体如下: 字典排序 按value排序 d1 = {"name":"python","bank":"icbc","country":china} # reverse是否倒序,x[1]代表value,x[0]为key d1 = sorted(d1.items(),lambda x: x[1],reverse=True) 按key排序 d1 = {&

  • Python实现的列表排序、反转操作示例

    本文实例讲述了Python实现的列表排序.反转操作.分享给大家供大家参考,具体如下: 排序: 使用sorted方法和列表的sort方法: sorted方法适用范围更广,sort方法只有列表有. li = [{'a':'23'}, {'a':'12'}] def sort_fun(mp): s = mp['a'] return int(s) print(sorted(li, key = sort_fun, reverse = True)) #这会返回一个排好序的列表,原列表不变. print(li

  • Python3实现对列表按元组指定列进行排序的方法分析

    本文实例讲述了Python3实现对列表按元组指定列进行排序的方法.分享给大家供大家参考,具体如下: Python版本: python3.+ 运行环境: Mac OS IDE: pycharm Python内建的排序方法 1 排序方法介绍 Python中有2个排序函数,一个是list内置的sort()方法,另一个是全局的sorted()方法 sorted(iterable,key=None,reverse=False) #返回排好序的新列表,不改变对象本身,默认升序;reverse:-True降序

  • Python列表常见操作详解(获取,增加,删除,修改,排序等)

    本文实例讲述了Python列表常见操作.分享给大家供大家参考,具体如下: 列表是由一系列按特定顺序排列的元素组成的对象.因为列表通常包含多个元素, 所以建议给列表指定一个表示复数的名称. 我们用方括号( [] ) 来表示列表, 并用逗号来分隔其中的元素. types=['娱乐','体育','科技'] print(types) 运行结果: ['娱乐', '体育', '科技'] 可以看到,打印列表的同时,也会将方括号打印出来. 1 获取元素 要获取列表中的某个元素, 在方括号内指定元素的索引即可:

  • python实现对象列表根据某个属性排序的方法详解

    本文实例讲述了python实现对象列表根据某个属性排序的方法.分享给大家供大家参考,具体如下: 对于一个已有的python list, 里面的内容是一些对象,这些对象有一些相同的属性值, 在一些特定的情况下,需要自己选择特定的排序,也就是根据某一个具体的属性来排序,在网上找了下资料,一般来说有两种方法,但从根本上来说,还是调用了list.sort 方法来实现.下面是简单的测试代码片段: #coding:utf-8 class Person: def __init__(self,name,age,

  • Python实现对特定列表进行从小到大排序操作示例

    本文实例讲述了Python实现对特定列表进行从小到大排序操作.分享给大家供大家参考,具体如下: 1.在系统内新建文件rizhireplacelist.txt root@kali:~# cd python/ root@kali:~/python# ls 111.txt           listsalaryver2.py  readfile2.py            rizhireplacelist.txt  rizhi.txt            tixingexcel.txt    

  • vue实现列表拖拽排序的示例代码

    本文主要介绍了vue实现列表拖拽排序的示例代码,具体如下: <template> <div class="test_wrapper" @dragover="dragover($event)"> <transition-group class="transition-wrapper" name="sort"> <div v-for="(item) in dataList&quo

  • react 实现表格列表拖拽排序的示例

    目录 问题描述 思路 解析 1. react-sortable-hoc 2. array-move 问题描述 在项目开发中,遇到这样一个需求:需要对表格里面的数据进行拖拽排序. 效果图如下所示: 思路 安装两个插件: react-sortable-hoc (或者 react-beautiful-dnd) array-move npm install --save react-sortable-hoc npm install --save array-move 解析 1. react-sortab

  • Elasticsearch聚合查询及排序操作示例

    目录 1 es排序 2 match和match的区别 3 分页查询 4 es 组合查询 5 结果过滤展示字端 6 结果高亮展示 7 聚合查询avg.max.min.sum.分组 8 mapping和_template模版 9 ik分词 10 term和match的区别 1 es排序 # 1 排序 GET jeff/doc/_search { "query": { "match": { "from": "gu" } }, &qu

  • Python实现的字典排序操作示例【按键名key与键值value排序】

    本文实例讲述了Python实现的字典排序操作.分享给大家供大家参考,具体如下: 对字典进行排序?这其实是一个伪命题,搞清楚python字典的定义---字典本身默认以key的字符顺序输出显示---就像我们用的真实的字典一样,按照abcd字母的顺序排列,并且本质上各自没有先后关系,是一个哈希表的结构: 但实际应用中我们确实有这种排序的"需求"-----按照values的值"排序"输出,或者按照别的奇怪的顺序进行输出,我们只需要把字典转化成list或者tuple,把字典每

  • Python实现字典按key或者value进行排序操作示例【sorted】

    本文实例讲述了Python实现字典按key或者value进行排序操作.分享给大家供大家参考,具体如下: 要点:使用到了python的内建函数与lambda函数 代码如下:(可直接复制运行) # -*- coding:utf-8 -*- #! python2 print '------定义一个字典d1---------------------------------------' d1 = {'a':14, 'c':12, 'b':11, 'e':13, 'f':16, 'd':15} print

  • Python使用lambda表达式对字典排序操作示例

    本文实例讲述了Python使用lambda表达式对字典排序操作.分享给大家供大家参考,具体如下: lambda表达式也常用于字典排序,既然写到字典排序,那就把按键排序和按值排序都写写好了. 字典按键排序 显然按键排序,需要用字典中每个元素的第一项排序 dict = {'a':1,'b':2,'c':3,'d':4,'e':3,'f':1,'g':7} sorted_dict_asc = sorted(dict.items(),key=lambda item:item[0]) sorted_dic

  • Python列表list排列组合操作示例

    本文实例讲述了Python列表list排列组合操作.分享给大家供大家参考,具体如下: 排列 例如: 输入为 ['1','2','3']和3 输出为 ['111','112','113','121','122','123','131','132','133','211','212','213','221','222','223','231','232','233','311','312','313','321','322','323','331','332','333'] 实现代码: # -*-

  • Python实现针对json中某个关键字段进行排序操作示例

    本文实例讲述了Python实现针对json中某个关键字段进行排序操作.分享给大家供大家参考,具体如下: 示例: json_array = [{"time":20150312,"value":"c"}, {"time":20150301,"value":"a"}, {"time":20150305,"value":"b"}] js

随机推荐