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 ...
随机推荐
- Install GD on mac
Lots of bioinformatics software are based on perl. Some of them, for example, Circos, NGS toolkit... ...
- Wamp安装使用+Git for Windows
相信很多朋友都曾在windows上做过web开发,我们常用的Web应用程序平台是:Apache+Mysql+Perl/PHP/Python,在windows下集成称为WAMP.web开发新手有时候由于 ...
- Android中GC_EXTERNAL_ALLOC的含义
GC_FOR_MALLOC means that the GC was triggered because there wasn't enough memory left on the heap to ...
- UI学习笔记---第八天
UINavigationController的用法 界面间传值 UInavigationController继承于UIViewController,以栈的方式管理所控制的师徒控制器,至少要有一个被 ...
- 时空上下文视觉跟踪(STC)算法的解读与代码复现(转)
时空上下文视觉跟踪(STC)算法的解读与代码复现 zouxy09@qq.com http://blog.csdn.net/zouxy09 本博文主要是关注一篇视觉跟踪的论文.这篇论文是Kaihua Z ...
- avalon框架
http://www.cnblogs.com/rubylouvre/p/4783966.html
- POJ 3422Kaka's Matrix Travels(最小费用最大流)
Kaka's Matrix Travels Time Limit: 1000MS M ...
- 【NOIP2014D2T3】解方程
神题一道,做了整整两天(其实是一个思路错了然后搞了两天QAQ) 原题: 已知多项式方程:a0+a1*x+a2*x^2+a3^x3+……+an^xn=0 求这个方程在[1, m]内的整数解(n 和 m ...
- springMvc源码学习之:spirngMvc获取请求参数的方法
一. 通过@PathVariabl获取路径中的参数 @RequestMapping(value="user/{id}/{name}",method=RequestMeth ...
- phpstrom 编辑器设置
http://www.jb51.net/article/58069.htm 配置sublime主题 保存配置的路径为:C:\Users\Administrator\.WebIde100\config ...