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


Posted in Python onFebruary 11, 2019

本文实例讲述了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]

Python 相关文章推荐
pyside写ui界面入门示例
Jan 22 Python
python获取android设备的GPS信息脚本分享
Mar 06 Python
Python实现简易Web爬虫详解
Jan 03 Python
Python实现的自定义多线程多进程类示例
Mar 23 Python
使用 Python 实现文件递归遍历的三种方式
Jul 18 Python
Django进阶之CSRF的解决
Aug 01 Python
python实现杨氏矩阵查找
Mar 02 Python
python xlwt如何设置单元格的自定义背景颜色
Sep 03 Python
python主线程与子线程的结束顺序实例解析
Dec 17 Python
详解python方法之绑定方法与非绑定方法
Aug 17 Python
python 如何把docker-compose.yaml导入到数据库相关条目里
Jan 15 Python
详解用Python把PDF转为Word方法总结
Apr 27 Python
实例讲解Python中浮点型的基本内容
Feb 11 #Python
实例介绍Python中整型
Feb 11 #Python
python开发准备工作之配置虚拟环境(非常重要)
Feb 11 #Python
pycharm配置pyqt5-tools开发环境的方法步骤
Feb 11 #Python
pycharm+PyQt5+python最新开发环境配置(踩坑)
Feb 11 #Python
Python requests模块实例用法
Feb 11 #Python
说说如何遍历Python列表的方法示例
Feb 11 #Python
You might like
php取得字符串首字母的方法
2015/03/25 PHP
php中二分法查找算法实例分析
2016/09/22 PHP
Yii遍历行下每列数据的方法
2016/10/17 PHP
php 使用curl模拟ip和来源进行访问的实现方法
2017/05/02 PHP
cnblogs 代码高亮显示后的代码复制问题解决实现代码
2011/12/14 Javascript
当自定义数据属性为json格式字符串时jQuery的data api问题探讨
2013/02/18 Javascript
完美兼容各大浏览器获取HTTP_REFERER方法总结
2014/06/24 Javascript
jQuery左右滚动支持图片放大缩略图图片轮播代码分享
2015/08/26 Javascript
Javascript6中字符串的四个新用法分享
2016/09/11 Javascript
JavaScript与ActionScript3两者的同性与差异性
2016/09/22 Javascript
JQuery Ajax WebService传递参数的简单实例
2016/11/02 Javascript
利用angularjs1.4制作的简易滑动门效果
2017/02/28 Javascript
JS中的作用域链
2017/03/01 Javascript
微信小程序实现tab左右切换效果
2020/11/15 Javascript
解决layui的form里的元素进行动态生成,验证失效的问题
2019/09/14 Javascript
vue使用showdown并实现代码区域高亮的示例代码
2019/10/17 Javascript
jquery实现加载更多"转圈圈"效果(示例代码)
2020/11/09 jQuery
python的即时标记项目练习笔记
2014/09/18 Python
谈谈Python进行验证码识别的一些想法
2016/01/25 Python
Python selenium抓取微博内容的示例代码
2018/05/17 Python
numpy中loadtxt 的用法详解
2018/08/03 Python
对sklearn的使用之数据集的拆分与训练详解(python3.6)
2018/12/14 Python
python自动化测试之DDT数据驱动的实现代码
2019/07/23 Python
PyTorch的torch.cat用法
2020/06/28 Python
Python中bisect的用法及示例详解
2020/07/20 Python
Python基于Faker假数据构造库
2020/11/30 Python
有个性的自我评价范文
2013/11/15 职场文书
园林资料员岗位职责
2013/12/30 职场文书
高中政治教学反思
2014/01/18 职场文书
中药专业自荐信范文
2014/03/18 职场文书
平安校园建设方案
2014/05/02 职场文书
拾金不昧表扬稿
2015/01/16 职场文书
计划生育个人总结
2015/03/02 职场文书
Python 实现定积分与二重定积分的操作
2021/05/26 Python
python实现A*寻路算法
2021/06/13 Python
一文搞懂PHP中的抽象类和接口
2022/05/25 PHP