日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python学习四(处理数据)

發(fā)布時(shí)間:2024/6/14 python 57 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python学习四(处理数据) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
python學(xué)習(xí)四(處理數(shù)據(jù))

head first python中的一個(gè)數(shù)據(jù)處理的例子

有四個(gè)U10選手的600米成績,請取出每個(gè)選手跑的最快的3個(gè)時(shí)間。以下是四位選手的9次成績

James

2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22

Julie

2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21

Mikey?

2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38

?Sarah

2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55

?代碼如下:

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)def get_coach_data(filename): try:with open(filename) as f:data = f.readline() return(data.strip().split(','))except IOError as ioerr:print('File error: ' + str(ioerr))return(None)james = get_coach_data('james.txt') julie = get_coach_data('julie.txt') mikey = get_coach_data('mikey.txt') sarah = get_coach_data('sarah.txt')print(sorted(set([sanitize(t) for t in james]))[0:3]) print(sorted(set([sanitize(t) for t in julie]))[0:3]) print(sorted(set([sanitize(t) for t in mikey]))[0:3]) print(sorted(set([sanitize(t) for t in sarah]))[0:3])

?

首先定義了一個(gè)模塊sanitize清理數(shù)據(jù),注意set集合中不允許重復(fù)記錄,sorted會返回一個(gè)排序后的列表,不會修改原有的列表。

?打印結(jié)果

['2.01', '2.22', '2.34']
['2.11', '2.23', '2.59']
['2.22', '2.38', '2.49']
['2.18', '2.25', '2.39']

?例2:

提供另外一組成績數(shù)據(jù),數(shù)據(jù)中包括了運(yùn)動員姓名,出生日期,及成績。

打印出每個(gè)運(yùn)動員姓名,及最快的三次成績?

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)def get_coach_data(filename):try:with open(filename) as f:data = f.readline()templ = data.strip().split(',')return({'Name' : templ.pop(0),'DOB' : templ.pop(0),'Times': str(sorted(set([sanitize(t) for t in templ]))[0:3])})except IOError as ioerr:print('File error: ' + str(ioerr))return(None)james = get_coach_data('james2.txt') julie = get_coach_data('julie2.txt') mikey = get_coach_data('mikey2.txt') sarah = get_coach_data('sarah2.txt')print(james['Name'] + "'s fastest times are: " + james['Times']) print(julie['Name'] + "'s fastest times are: " + julie['Times']) print(mikey['Name'] + "'s fastest times are: " + mikey['Times']) print(sarah['Name'] + "'s fastest times are: " + sarah['Times'])

?上面代碼中用{}定義了一個(gè)map類型的數(shù)據(jù)結(jié)構(gòu),key分別是name,DOB,Times。

也可以用其它方式實(shí)現(xiàn),類似于JAVA中的JAVABEAN

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)class AthleteList(list):def __init__(self, a_name, a_dob=None, a_times=[]):list.__init__([])self.name = a_nameself.dob = a_dobself.extend(a_times)def top3(self):return(sorted(set([sanitize(t) for t in self]))[0:3])def get_coach_data(filename):try:with open(filename) as f:data = f.readline()templ = data.strip().split(',')return(AthleteList(templ.pop(0), templ.pop(0), templ))except IOError as ioerr:print('File error: ' + str(ioerr))return(None)james = get_coach_data('james2.txt') julie = get_coach_data('julie2.txt') mikey = get_coach_data('mikey2.txt') sarah = get_coach_data('sarah2.txt')print(james.name + "'s fastest times are: " + str(james.top3())) print(julie.name + "'s fastest times are: " + str(julie.top3())) print(mikey.name + "'s fastest times are: " + str(mikey.top3())) print(sarah.name + "'s fastest times are: " + str(sarah.top3()))

?

注意class中的每個(gè)方法的第一個(gè)參數(shù)必須是self?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

posted on 2013-11-23 23:22 pingh14 閱讀(...) 評論(...) 編輯 收藏

轉(zhuǎn)載于:https://www.cnblogs.com/pingh/p/3439601.html

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的python学习四(处理数据)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。