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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python 字典查询比列表快_Python 字典和列表的对比应用

發(fā)布時間:2025/3/15 python 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 字典查询比列表快_Python 字典和列表的对比应用 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Q:將下列格式的txt文件,打印出該選手的3個最快跑步時間

james2.txt =>“James Lee,2002-3-14,2-34,3:21,2.34,2.45,3.01,2:01,2:01,3:10,2-22,2-01,2.01,2:16"

julie2.txt =>Julie Jones,2002-8-17,2.59,2.11,2:11,2:23,3-10,2-23,3:10,3.21,3-21,3.01,3.02,2:59

mikey2.txt =>Mikey McManus,2002-2-24,2:22,3.01,3:01,3.02,3:02,3.02,3:22,2.49,2:38,2:40,2.22,2-31

sarah2.txt =>Sarah Sweeney,2002-6-17,2:58,2.58,2:39,2-25,2-55,2:54,2.18,2:55,2:55,2:22,2-21,2.22

注.pop()方法從指定列表位置刪除并返回一個數(shù)據(jù)項。

1.通過數(shù)據(jù)抽取到列表來實現(xiàn)

def senitize(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)

sarah=get_coach_data('sarah2.txt')

(sarah_name,sarah_dob)=sarah.pop(0), sarah.pop(0)

print(sarah_name+"'s fastest times are:"+ str(sorted(set([senitize(t) for t in sarah]))[0:3]))

========== RESTART: C:/Users/eric/Documents/Python/kelly/kelly2.py ==========

Sarah Sweeney's fastest times are:['2.18', '2.21', '2.22']

2. 通過創(chuàng)建字典來實現(xiàn)

def senitize(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)

sarah=get_coach_data('sarah2.txt')

sarah_data={}

sarah_data['Name']=sarah.pop(0)

sarah_data['DOB']=sarah.pop(0)

sarah_data['Times']=sarah

print(sarah_data['Name']+"'s fastest times are:"+ str(sorted(set([senitize(t) for t in sarah_data['Times']]))[0:3]))

3.將字典和數(shù)據(jù)打印全部寫入函數(shù)

def senitize(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()

user=data.strip().split(',')

user_data={}

user_data['Name']=user.pop(0)

user_data['DOB']=user.pop(0)

user_data['Times']=user

print(user_data['Name']+"'s fastest times are:"+ str(sorted(set([senitize(t) for t in user_data['Times']]))[0:3]))

except IOError as ioerr:

print('File error' +str (ioerr))

return (None)

get_coach_data('sarah2.txt')

get_coach_data('james2.txt')

get_coach_data('mikey2.txt')

get_coach_data('julie2.txt')

========== RESTART: C:/Users/eric/Documents/Python/kelly/kelly2.py ==========

Sarah Sweeney's fastest times are:['2.18', '2.21', '2.22']

James Lee's fastest times are:['2.01', '2.16', '2.22']

Mikey McManus's fastest times are:['2.22', '2.31', '2.38']

Julie Jones's fastest times are:['2.11', '2.23', '2.59']

總結

以上是生活随笔為你收集整理的python 字典查询比列表快_Python 字典和列表的对比应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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