python学习四(处理数据)
生活随笔
收集整理的這篇文章主要介紹了
python学习四(处理数据)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
python學習四(處理數據)
head first python中的一個數據處理的例子
有四個U10選手的600米成績,請取出每個選手跑的最快的3個時間。以下是四位選手的9次成績
James
2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22Julie
2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21Mikey?
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])?
首先定義了一個模塊sanitize清理數據,注意set集合中不允許重復記錄,sorted會返回一個排序后的列表,不會修改原有的列表。
?打印結果
['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:
提供另外一組成績數據,數據中包括了運動員姓名,出生日期,及成績。
打印出每個運動員姓名,及最快的三次成績?
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'])?上面代碼中用{}定義了一個map類型的數據結構,key分別是name,DOB,Times。
也可以用其它方式實現,類似于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中的每個方法的第一個參數必須是self?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
posted on 2013-11-23 23:22 pingh14 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/pingh/p/3439601.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的python学习四(处理数据)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ref out
- 下一篇: 通过Log4net来配置我们需要的日志文