head first python读书笔记(十一)
2015-09-24 16:13
597 查看
1、这算是第一个自己编的程序,因为自己的太繁琐,所以没贴上来,比较书上的来看,区别在两个地方:第一,是用了with,就不用再close了;第二,是用了方法串链,data.strip().split(',')
>>> with open('james.txt') as jaf:
data=jaf.readline()
james=data.strip().split(',')
with open('julie.txt') as juf:
data=juf.readline()
julie=data.strip().split(',')
with open('mikey.txt') as mif:
data=mif.readline()
mikey=data.strip().split(',')
with open('sarah.txt') as saf:
data=saf.readline()
sarah=data.strip().split(',')
>>> print(james)
['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22']
>>> print(julie)
['2.59', '2.11', '2:11', '2:23', '3-10', '2-23', '3:10', '3.21', '3-21']
>>> print(mikey)
['2:22', '3.01', '3:01', '3.02', '3:02', '3.02', '3:22', '2.49', '2:38']
>>> print(sarah)
['2:58', '2.58', '2:39', '2-25', '2-55', '2:54', '2.18', '2:55', '2:55']
2、排序,sort是在原来的列表上排列,sorted是排列后保存到副本中
>>> data=[6,3,1,2,4,5]
>>> data
[6, 3, 1, 2, 4, 5]
>>> data1=sorted(data)
>>> data1
[1, 2, 3, 4, 5, 6]
>>> data
[6, 3, 1, 2, 4, 5]
>>> data.sort()
>>> data
[1, 2, 3, 4, 5, 6]
3、数据处理(转换)
>>>import zh
>>> with open('james.txt') as jaf:
data=jaf.readline()
james=data.strip().split(',')
with open('julie.txt') as juf:
data=juf.readline()
julie=data.strip().split(',')
with open('mikey.txt') as mif:
data=mif.readline()
mikey=data.strip().split(',')
with open('sarah.txt') as saf:
data=saf.readline()
sarah=data.strip().split(',')
>>> for each_t in james:
clean_james.append(zh.sanitize(each_t))
>>> for each_t in julie:
clean_julie.append(zh.sanitize(each_t))
>>> for each_t in mikey:
clean_mikey.append(zh.sanitize(each_t))
>>> for each_t in sarah:
clean_sarah.append(zh.sanitize(each_t))
>>> print(sorted(clean_james))
['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']
>>> print(sorted(clean_julie))
['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']
>>> print(sorted(clean_sarah))
['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']
>>> print(sorted(clean_mikey))
['2.22', '2.38', '2.49', '3.01', '3.01', '3.02', '3.02', '3.02', '3.22']
'''zh.py'''
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)
4、列表推导:
>>> for each_t in james:
clean_james1=[zh.sanitize(each_t) for each_t in james]
目前见过的最麻烦的列表推导(涉及到了列表推导、字符串转换为浮点数、合并对数据项的转换)
>>> clean=[float(zh.sanitize(t)) for t in ['2-22','3:33','4.44']]
>>> clean
[2.22, 3.33, 4.44]
5、利用列表推导,把之前的题目精简了不只一丢丢
>>> print(sorted([zh.sanitize(t) for t in james]))
['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']
>>> print(sorted([zh.sanitize(t) for t in julie]))
['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']
>>> print(sorted([zh.sanitize(t) for t in sarah]))
['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']
>>> print(sorted([zh.sanitize(t) for t in mikey]))
6、迭代删除重复项
>>> unique_mikey=[]
>>> for each_t in mikey:
if each_t not in unique_mikey:
unique_mikey.append(each_t)
>>> unique_mikey
['2.22', '2.38', '2.49', '3.01', '3.02', '3.22']
7、输出前三项
>>> print(unique_mikey[0:3])
['2.22', '2.38', '2.49']
8、集合删除重复项
>>> distance=set(mikey)
>>> distance
{'2.49', '2.22', '2.38', '3.02', '3.22', '3.01'}
9、可以直接用set删除重复项,然后sorted排序,然后[0:3]取前3个
>>> print(sorted(set(mikey))[0:3])
['2.22', '2.38', '2.49']
>>> sorted(set(mikey))[0:3]
['2.22', '2.38', '2.49']
10、最终版本
>>> import wbwj
>>> import zh
>>> james=wbwj.dk('james.txt')
>>> julie=wbwj.dk('julie.txt')
>>> sarah=wbwj.dk('sarah.txt')
>>> mikey=wbwj.dk('mikey.txt')
>>> print(sorted(set([zh.sanitize(t) for t in james]))[0:3])
['2.01', '2.22', '2.34']
>>> print(sorted(set([zh.sanitize(t) for t in julie]))[0:3])
['2.11', '2.23', '2.59']
>>> print(sorted(set([zh.sanitize(t) for t in sarah]))[0:3])
['2.18', '2.25', '2.39']
>>> print(sorted(set([zh.sanitize(t) for t in mikey]))[0:3])
['2.22', '2.38', '2.49']
wbwj.py
def dk(file):
with open(file) as inpt:
data=inpt.readline()
return(data.strip().split(','))
zh.py
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 jaf:
data=jaf.readline()
james=data.strip().split(',')
with open('julie.txt') as juf:
data=juf.readline()
julie=data.strip().split(',')
with open('mikey.txt') as mif:
data=mif.readline()
mikey=data.strip().split(',')
with open('sarah.txt') as saf:
data=saf.readline()
sarah=data.strip().split(',')
>>> print(james)
['2-34', '3:21', '2.34', '2.45', '3.01', '2:01', '2:01', '3:10', '2-22']
>>> print(julie)
['2.59', '2.11', '2:11', '2:23', '3-10', '2-23', '3:10', '3.21', '3-21']
>>> print(mikey)
['2:22', '3.01', '3:01', '3.02', '3:02', '3.02', '3:22', '2.49', '2:38']
>>> print(sarah)
['2:58', '2.58', '2:39', '2-25', '2-55', '2:54', '2.18', '2:55', '2:55']
2、排序,sort是在原来的列表上排列,sorted是排列后保存到副本中
>>> data=[6,3,1,2,4,5]
>>> data
[6, 3, 1, 2, 4, 5]
>>> data1=sorted(data)
>>> data1
[1, 2, 3, 4, 5, 6]
>>> data
[6, 3, 1, 2, 4, 5]
>>> data.sort()
>>> data
[1, 2, 3, 4, 5, 6]
3、数据处理(转换)
>>>import zh
>>> with open('james.txt') as jaf:
data=jaf.readline()
james=data.strip().split(',')
with open('julie.txt') as juf:
data=juf.readline()
julie=data.strip().split(',')
with open('mikey.txt') as mif:
data=mif.readline()
mikey=data.strip().split(',')
with open('sarah.txt') as saf:
data=saf.readline()
sarah=data.strip().split(',')
>>> for each_t in james:
clean_james.append(zh.sanitize(each_t))
>>> for each_t in julie:
clean_julie.append(zh.sanitize(each_t))
>>> for each_t in mikey:
clean_mikey.append(zh.sanitize(each_t))
>>> for each_t in sarah:
clean_sarah.append(zh.sanitize(each_t))
>>> print(sorted(clean_james))
['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']
>>> print(sorted(clean_julie))
['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']
>>> print(sorted(clean_sarah))
['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']
>>> print(sorted(clean_mikey))
['2.22', '2.38', '2.49', '3.01', '3.01', '3.02', '3.02', '3.02', '3.22']
'''zh.py'''
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)
4、列表推导:
>>> for each_t in james:
clean_james1=[zh.sanitize(each_t) for each_t in james]
目前见过的最麻烦的列表推导(涉及到了列表推导、字符串转换为浮点数、合并对数据项的转换)
>>> clean=[float(zh.sanitize(t)) for t in ['2-22','3:33','4.44']]
>>> clean
[2.22, 3.33, 4.44]
5、利用列表推导,把之前的题目精简了不只一丢丢
>>> print(sorted([zh.sanitize(t) for t in james]))
['2.01', '2.01', '2.22', '2.34', '2.34', '2.45', '3.01', '3.10', '3.21']
>>> print(sorted([zh.sanitize(t) for t in julie]))
['2.11', '2.11', '2.23', '2.23', '2.59', '3.10', '3.10', '3.21', '3.21']
>>> print(sorted([zh.sanitize(t) for t in sarah]))
['2.18', '2.25', '2.39', '2.54', '2.55', '2.55', '2.55', '2.58', '2.58']
>>> print(sorted([zh.sanitize(t) for t in mikey]))
6、迭代删除重复项
>>> unique_mikey=[]
>>> for each_t in mikey:
if each_t not in unique_mikey:
unique_mikey.append(each_t)
>>> unique_mikey
['2.22', '2.38', '2.49', '3.01', '3.02', '3.22']
7、输出前三项
>>> print(unique_mikey[0:3])
['2.22', '2.38', '2.49']
8、集合删除重复项
>>> distance=set(mikey)
>>> distance
{'2.49', '2.22', '2.38', '3.02', '3.22', '3.01'}
9、可以直接用set删除重复项,然后sorted排序,然后[0:3]取前3个
>>> print(sorted(set(mikey))[0:3])
['2.22', '2.38', '2.49']
>>> sorted(set(mikey))[0:3]
['2.22', '2.38', '2.49']
10、最终版本
>>> import wbwj
>>> import zh
>>> james=wbwj.dk('james.txt')
>>> julie=wbwj.dk('julie.txt')
>>> sarah=wbwj.dk('sarah.txt')
>>> mikey=wbwj.dk('mikey.txt')
>>> print(sorted(set([zh.sanitize(t) for t in james]))[0:3])
['2.01', '2.22', '2.34']
>>> print(sorted(set([zh.sanitize(t) for t in julie]))[0:3])
['2.11', '2.23', '2.59']
>>> print(sorted(set([zh.sanitize(t) for t in sarah]))[0:3])
['2.18', '2.25', '2.39']
>>> print(sorted(set([zh.sanitize(t) for t in mikey]))[0:3])
['2.22', '2.38', '2.49']
wbwj.py
def dk(file):
with open(file) as inpt:
data=inpt.readline()
return(data.strip().split(','))
zh.py
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)
相关文章推荐
- 猪猪侠那里弄的python递归脚本
- Python 网络爬虫 - 抓取糗事百科的段子(最新版)
- [LeetCode]题解(python):017-Letter Combinations of a Phone Number
- 【Python】BeautifulSoup文档(windows-pyhton2.7.10)
- python多进程(multiprocessing)
- 零基础学python-19.4 为什么要在意列表解析与序列映射函数map
- 零基础学python-19.4 为什么要在意列表解析与序列映射函数map
- python批量连接mysql
- 八大排序算法的Python实现
- [LeetCode]题解(python):016-3Sum Closest
- python应用之求主析取范式,主合取范式
- Python try/except异常处理机制
- Python--基本文件方法
- python gzip
- 关于使用python Process 参数消除歧义
- Python分析C盘大文件[夹]
- Python 通过print将数据保存到文件中
- Python opencv 调用sift
- sublime配置python运行环境
- 记录python ConfigParser 处理编码问题