您的位置:首页 > 编程语言 > Python开发

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: