Python 数据排序和列表迭代和列表推导应用
2024-09-20 20:24:13
1.In-place sorting 原地排序
data=[6,4,5,2,3,1]
print ('before sort', data)
data.sort()
print ('after sort BIF:', data) =========== RESTART: C:/Users/eric/Documents/Python/kelly/sort.py ===========
before sort [6, 4, 5, 2, 3, 1]
after sort BIF: [1, 2, 3, 4, 5, 6]
2. copied sorting 复制排序
test=[6,4,5,2,3,1]
print ('before sorted', test)
test2=sorted(test)
print ('after sorted BIF, test', test)
print ('after sorted BIF, test2',test2) =========== RESTART: C:/Users/eric/Documents/Python/kelly/sort.py ===========
before sorted [6, 4, 5, 2, 3, 1]
after sorted BIF, test [6, 4, 5, 2, 3, 1]
after sorted BIF, test2 [1, 2, 3, 4, 5, 6]
3. use senitize func 列表迭代处理各个选手的列表数据,将清理过的值追加到适当新列表
def sanitize(time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return (time_string)
(mins, secs)=time_string.split(splitter)
return(mins + '.' + secs) with open ('james.txt') as jas: data = jas.readline()
james=data.strip().split(',') with open('julie.txt') as jue: data=jue.readline()
julie=data.strip().split(',') with open('mikey.txt') as miy: data=miy.readline()
mikey=data.strip().split(',') with open('sarah.txt') as sah: data=sah.readline()
sarah=data.strip().split(',') print ('before sort and clean data' ,james,julie,mikey,sarah) clean_james=[]
clean_julie=[]
clean_mikey=[]
clean_sarah=[] for each_t in james:
clean_james.append(sanitize(each_t))
for each_t in julie:
clean_julie.append(sanitize(each_t))
for each_t in mikey:
clean_mikey.append(sanitize(each_t))
for each_t in sarah:
clean_sarah.append(sanitize(each_t)) print('after clean and sorted james is :',sorted(clean_james))
print('after clean and sorted julie is :',sorted(clean_julie))
print('after clean and sorted mikey is :',sorted(clean_mikey))
print('after clean and sorted sarah is :',sorted(clean_sarah)) =========== RESTART: C:\Users\eric\Documents\Python\kelly\kelly.py ===========
before sort and clean data ['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22'] ['2.59', '2.11', '2:11', '2:23', '3-10', '2-23', '3:10', '3.21', '3-21'] ['2:22', '3.01', '3:01', '3.02', '3:02', '3.02', '3:22', '2.49', '2:38'] ['2:58', '2.58', '2:39', '2-25', '2-55', '2:54', '2.18', '2:55', '2:55']
after clean and sorted james is : ['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']
after clean and sorted julie is : ['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']
after clean and sorted mikey is : ['2.22', '2.38', '2.49', '3.01', '3.01', '3.02', '3.02', '3.02', '3.22']
after clean and sorted sarah is : ['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']
4.list comprehension 运用 “列表推导”减少代码,达到同样效果
def sanitize(time_string):
if '-' in time_string:
splitter = '-'
elif ':' in time_string:
splitter = ':'
else:
return (time_string)
(mins, secs)=time_string.split(splitter)
return(mins + '.' + secs) with open ('james.txt') as jas: data = jas.readline()
james=data.strip().split(',')
with open('julie.txt') as jue: data=jue.readline()
julie=data.strip().split(',')
with open('mikey.txt') as miy: data=miy.readline()
mikey=data.strip().split(',')
with open('sarah.txt') as sah: data=sah.readline()
sarah=data.strip().split(',') print ('before sort and clean data' ,james,julie,mikey,sarah) clean_james=[sanitize(each_t) for each_t in james]
clean_julie=[sanitize(each_t) for each_t in julie]
clean_mikey=[sanitize(each_t) for each_t in mikey]
clean_sarah=[sanitize(each_t) for each_t in sarah] print('after clean and sorted james is :',sorted(clean_james))
print('after clean and sorted julie is :',sorted(clean_julie))
print('after clean and sorted mikey is :',sorted(clean_mikey))
print('after clean and sorted sarah is :',sorted(clean_sarah)) >>>
=========== RESTART: C:\Users\eric\Documents\Python\kelly\kelly.py ===========
before sort and clean data ['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22'] ['2.59', '2.11', '2:11', '2:23', '3-10', '2-23', '3:10', '3.21', '3-21'] ['2:22', '3.01', '3:01', '3.02', '3:02', '3.02', '3:22', '2.49', '2:38'] ['2:58', '2.58', '2:39', '2-25', '2-55', '2:54', '2.18', '2:55', '2:55']
after clean and sorted james is : ['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']
after clean and sorted julie is : ['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']
after clean and sorted mikey is : ['2.22', '2.38', '2.49', '3.01', '3.01', '3.02', '3.02', '3.02', '3.22']
after clean and sorted sarah is : ['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']
最新文章
- SQL Server 数据加密功能解析
- Google V8编程详解(序)Cloud App
- 【转】【MySQL】mysql 通过bin-log恢复数据方法详解
- 关于win10输入法问题(打不出中文)解决方法
- GIT warning: LF will be replaced by CRLF.
- A*算法入门
- django inclusion_tag
- hdu 1022 Train Problem I 解题报告
- UIlabel 显示模糊
- [Java] final的意义
- 自定义xcode文件模板
- DedeCMS V5.7开启memcache缓存的方法配置说明
- android小说阅读源码、bilibili源码、MVP新闻源码等
- django-restframework之缓存系统
- js入门关于函数
- laravel 模型操作
- 使用Sharding-Proxy进行分库分表
- Android Hook框架adbi源码浅析(二)
- Three.js开发指南---粒子和粒子系统(第七章)
- 本博客已经迁移去http://blog.brightwang.com/