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']

最新文章

  1. SQL Server 数据加密功能解析
  2. Google V8编程详解(序)Cloud App
  3. 【转】【MySQL】mysql 通过bin-log恢复数据方法详解
  4. 关于win10输入法问题(打不出中文)解决方法
  5. GIT warning: LF will be replaced by CRLF.
  6. A*算法入门
  7. django inclusion_tag
  8. hdu 1022 Train Problem I 解题报告
  9. UIlabel 显示模糊
  10. [Java] final的意义
  11. 自定义xcode文件模板
  12. DedeCMS V5.7开启memcache缓存的方法配置说明
  13. android小说阅读源码、bilibili源码、MVP新闻源码等
  14. django-restframework之缓存系统
  15. js入门关于函数
  16. laravel 模型操作
  17. 使用Sharding-Proxy进行分库分表
  18. Android Hook框架adbi源码浅析(二)
  19. Three.js开发指南---粒子和粒子系统(第七章)
  20. 本博客已经迁移去http://blog.brightwang.com/

热门文章

  1. CUDA 并行编程简介
  2. word文档快速取消图片的链接
  3. Linux 安装挂载时注意事项
  4. Sql优化(二) 快速计算Distinct Count
  5. linux下ftp常用命令
  6. 安装sklearn时出现 "ImportError: DLL load failed" 的解决方法
  7. GRANT ALL PRIVILEGES 限制某个或所有客户端都可以连接至mysql
  8. Vue.js相关知识1
  9. Linux scp 远程文件/目录传输
  10. ExtJS 的一些技巧与问题