Python 数据排序和列表迭代和列表推导应用
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']
Python 数据排序和列表迭代和列表推导应用的更多相关文章
- 送你一个Python 数据排序的好方法
摘要:学习 Pandas排序方法是开始或练习使用 Python进行基本数据分析的好方法.最常见的数据分析是使用电子表格.SQL或pandas 完成的.使用 Pandas 的一大优点是它可以处理大量数据 ...
- Python高级特性(切片,迭代,列表生成式,生成器,迭代器)
掌握了Python的数据类型.语句和函数,基本上就可以编写出很多有用的程序了. 比如构造一个1, 3, 5, 7, ..., 99的列表,可以通过循环实现: L = [] n = 1 while n ...
- python数据排序
1.原地排序 data.sort() #对原列表进行排序 2.复制排序 data2 = sorted(data) #原列表不变,作为参数传给sorted()方法进行排序
- Python的排序
1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a','have','I' ...
- python 字典排序 关于sort()、reversed()、sorted()
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- <转>python字典排序 关于sort()、reversed()、sorted()
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- python 字典排序,列表排序详细
在程序中使用字典进行数据信息统计时,由于字典是无序的所以打印字典时内容也是无序的.因此,为了使统计得到的结果更方便查看需要进行排序.Python中字典的排序分为按“键”排序和按“值”排序. 1.按“值 ...
- Python 迭代器之列表解析
 [TOC] 尽管while和for循环能够执行大多数重复性任务, 但是由于序列的迭代需求如此常见和广泛, 以至于Python提供了额外的工具以使其更简单和高效. 迭代器在Python中是以C语言的 ...
- Python入门基础之迭代和列表生成式
什么是迭代 在Python中,如果给定一个list或tuple,我们可以通过for循环来遍历这个list或tuple,这种遍历我们成为迭代(Iteration). 在Python中,迭代是通过 for ...
随机推荐
- centos 用户组
centos 用户组和用户的添加及所属问题 指定附加组 eg:dy 为组名 ,lisi 为用户,要把lisi 用户添加到 dy 组里面 useradd -G dy lisi 可以同时添加多个组, ...
- Centos6下DRBD的安装配置
导读 Distributed Replicated Block Device(DRBD)是一个用软件实现的.无共享的.服务器之间镜像块设备内容的存储复制解决方案.数据镜像:实时.透明.同步(所有服务器 ...
- Notes of Principles of Parallel Programming: Peril-L Notation - TODO
Content 1 syntax and semantic 2 example set 1 syntax and semantic 1.1 extending C Peril-L notation s ...
- 38. Count and Say
The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221 ...
- Sprint第二个冲刺(第一天)
因为人员变动关系,我们的博客推迟了两天发布,希望老师能够谅解. 现在“广商百货”团队项目的新的团队成员组成为:董婷婷(组长).容杰龙.卓炜杰.袁文洪和吴建明 在经过第一轮和几天的休息,现在我们准备开始 ...
- HDU 1392 凸包
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- 关于SSH整合中对于Hibernate的Session关闭的问题
在web.xml的Struts2的配置上面加上 <filter> <filter-name>OpenSessionInViewFilter</filter-name> ...
- WindowsServer问题总结
1.System.Security.SecurityException: 未找到源,但未能搜索某些或全部事件日志.不可访问的日志: Security.在安装的“回滚”阶段发生异常.将忽略该异常并继续回 ...
- Docker简介
Docker简介 1.容器虚拟化,比传统的虚拟化轻量 2.2013年出现,发展非常迅猛 3.Redhat在6.5版本开始支持docker 4.使用go语言开发,基于apache2.0协议 5.开源原件 ...
- html 绑定
html 绑定 目的 html绑定到DOM元素上,使得该元素显示的HTML值为你绑定的参数.如果在你的view model里声明HTML标记并且render的话,那非常有用. 例子 <div ...