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 相关文章推荐
python正则匹配查询港澳通行证办理进度示例分享
Dec 27 Python
详解python进行mp3格式判断
Dec 23 Python
python如何实现反向迭代
Mar 20 Python
Python入门学习指南分享
Apr 11 Python
Python3之简单搭建自带服务器的实例讲解
Jun 04 Python
Python使用pickle模块储存对象操作示例
Aug 15 Python
python找出完数的方法
Nov 12 Python
Python中的 sort 和 sorted的用法与区别
Aug 10 Python
python中类的输出或类的实例输出为这种形式的原因
Aug 12 Python
numpy.linalg.eig() 计算矩阵特征向量方式
Nov 29 Python
Pytorch 使用opnecv读入图像由HWC转为BCHW格式方式
Jun 02 Python
PyQt5的相对布局管理的实现
Aug 07 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中常见的sql攻击正则表达式汇总
2014/11/06 PHP
PHP获取用户访问IP地址的5种方法
2016/05/16 PHP
yii2 modal弹窗之ActiveForm ajax表单异步验证
2016/06/13 PHP
jquery 子窗口操作父窗口的代码
2009/09/21 Javascript
Jquery插件之打造自定义的select标签
2011/11/30 Javascript
节点的插入之append()和appendTo()的用法介绍
2014/01/13 Javascript
jquery中checkbox全选失效的解决方法
2014/12/26 Javascript
JS组件Form表单验证神器BootstrapValidator
2016/01/26 Javascript
JavaScript实现点击单元格改变背景色的方法
2016/02/12 Javascript
JavaScript模拟文件拖选框样式v1.0的实例
2017/08/04 Javascript
Vue中util的工具函数实例详解
2019/07/08 Javascript
layui table设置某一行的字体颜色方法
2019/09/05 Javascript
[52:03]Secret vs VG 2018国际邀请赛小组赛BO2 第一场 8.17
2018/08/20 DOTA
[07:01]DOTA2-DPC中国联赛正赛 Aster vs Magma 3月5日 赛后选手采访
2021/03/11 DOTA
Python实现Pig Latin小游戏实例代码
2018/02/02 Python
Python使用win32 COM实现Excel的写入与保存功能示例
2018/05/03 Python
Python函数定义及传参方式详解(4种)
2019/03/18 Python
详解python校验SQL脚本命名规则
2019/03/22 Python
python 基于TCP协议的套接字编程详解
2019/06/29 Python
使用TensorFlow直接获取处理MNIST数据方式
2020/02/10 Python
TensorBoard 计算图的查看方式
2020/02/15 Python
Ubuntu20下的Django安装的方法步骤
2021/01/24 Python
贝玲妃美国官方网站:Benefit美国
2016/08/28 全球购物
日本7net购物网:书籍、漫画、杂志、DVD、游戏邮购
2017/02/17 全球购物
Lowe’s加拿大:家居装修、翻新和五金店
2019/12/06 全球购物
什么是SQL Server的确定性函数和不确定性函数
2016/08/04 面试题
葡萄牙语专业个人求职信
2013/12/10 职场文书
中专生职业生涯规划书范文
2014/01/10 职场文书
工地质量标语
2014/06/12 职场文书
教师职位说明书
2014/07/29 职场文书
关于环保的活动方案
2014/08/25 职场文书
计划生育工作汇报
2014/10/28 职场文书
投资公司董事长岗位职责
2015/04/16 职场文书
2016年全国爱眼日宣传教育活动总结
2016/04/05 职场文书
《烈火英雄》观后感:致敬和平时代的英雄
2019/11/11 职场文书
Python requests库参数提交的注意事项总结
2021/03/29 Python