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 7 上安装广告服务器 Revive Adserver
导读 Revive Adserver是一个自由开源的广告管理系统,能使出版商,广告平台和广告商在网页.应用.视频上投放并管理广告的系统.Revive Adserver以前叫做OpenX Source, ...
- 11. Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- 153. Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- SimPholders2 模拟器 App 文件路径查看工具
SimPholder2.app 官网下载地址:http://www.simpholders.com 当使用 Xcode beta 版本切换到 Xcode 正式版本时,点击 SimPholders2. ...
- Oracle 删除用户和表空间
版权声明:本文为博主原创文章,未经博主允许不得转载. Oracle 使用时间长了, 新增了许多user 和tablespace. 需要清理一下 对于单个user和tablespace 来说, 可以使用 ...
- console下纯字符实现的贪吃蛇
最近简直超级无聊-- code blocks win7 64编译运行无问题,应该其他编译器也不会有问题. w:上 s:下 a:左 d:右 CS标准方向控制,AK47和M4这种高级货是没有滴-- 废话不 ...
- C# App.config 自定义 配置节 报错“配置系统未能初始化” 解决方法
App.config,结果运行的时候出现了 "配置系统未能初始化" 的错误.找了半天才发现是下面的原因造成的: "如果配置文件中包含configSections元素,则c ...
- [codeforces 391D2]Supercollider
这题觉得似乎不难的样子…… 但硬是没有做出来,真是不知说什么好喵~ 注意到没有两条共线的线段具有公共点,没有重合的线段 说明每个十字形最多涉及一个水平线段和一个竖直线段 这说明我们可以二分了:每条线段 ...
- 浅谈 Active Learning
1. Active Query Driven by Uncertainty and Diversity for Incremental Multi-Label Learning The key tas ...
- ie6 z-index(用父元素的优先级来解决)
父元素的优先级决定了子元素的优先级