python对列表排序(Python实现对特定列表进行从小到大排序操作示例)
python对列表排序
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]
希望本文所述对大家Python程序设计有所帮助。
原文链接:https://blog.csdn.net/xwbk12/article/details/72783272
- python 爬虫图形验证码(Python爬虫实现验证码登录代码实例)
- pythonhtml文件分析(对Python3 解析html的几种操作方式小结)
- python统计出现文字最多的词(使用Python 统计高频字数的方法)
- 如何用python做一个弹窗(python实现弹窗祝福效果)
- eval详解python(深度辨析Python的eval与exec的方法)
- python扫描服务器端口(Python实现的对本地host127.0.0.1主机进行扫描端口功能示例)
- python怎么设置matlab编程(实例详解Matlab 与 Python 的区别)
- python绘折线图数据(python2.7使用plotly绘制本地散点图和折线图)
- python正则表达式入门(Python正则表达式实现简易计算器功能示例)
- python中生成字符串序列(python实现字符串加密 生成唯一固定长度字符串)
- python静态方法和类方法的区别(Python实例方法、类方法、静态方法的区别与作用详解)
- python代码返回值(Python 函数返回值的示例代码)
- python制作彩色字符(Python3利用print输出带颜色的彩色字体示例代码)
- python函数大全详细(详解Python函数式编程—高阶函数)
- opencv调用摄像头图像识别(Python+OpenCV采集本地摄像头的视频)
- python读写文件实验心得(Python文件读写常见用法总结)
- 刚红就耍大牌,《琉璃》角色滤镜碎一地,心疼工作人员(琉璃角色滤镜碎一地)
- 袁冰妍郑业成这对可以处,有脸红情话他们是真的敢说(袁冰妍郑业成这对可以处)
- 《祝卿好》台词又土又甜,就喜欢这么直接的恋爱(祝卿好台词又土又甜)
- 大女主 汤唯垂青电视圈,搭档朱亚文出演《大明皇妃孙若微传》(汤唯垂青电视圈)
- 红色代表什么(红色代表什么情感和含义)
- 高中数学题(高中数学题型总结及解题方法)
热门推荐
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9