scores={}
result_f=open("results.txt")
for line in result_f:
(name,score)=line.split()
scores[score]=name
result_f.close()
print("The top scores were:")
for each_score in scores.keys():
print('姓名:'+scores[each_score]+'分數:'+each_score)
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
The top scores were:
姓名:Stacey分數:7.81
姓名:Aideen分數:8.05
姓名:Joseph分數:8.45
姓名:Johnny分數:8.65
姓名:Zack分數:7.12
姓名:Juan分數:9.12
姓名:Aaron分數:8.31
>>>
加入排序方法:
scores={}
result_f=open("results.txt")
for line in result_f:
(name,score)=line.split()
scores[score]=name
result_f.close()
print("The top scores were:")
for each_score in sorted(scores.keys(),reverse=True):
print('姓名:'+scores[each_score]+'分數:'+each_score)
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
The top scores were:
姓名:Stacey分數:7.81
姓名:Aideen分數:8.05
姓名:Joseph分數:8.45
姓名:Johnny分數:8.65
姓名:Zack分數:7.12
姓名:Juan分數:9.12
姓名:Aaron分數:8.31
>>> ================================ RESTART ================================
>>>
The top scores were:
姓名:Juan分數:9.12
姓名:Johnny分數:8.65
姓名:Joseph分數:8.45
姓名:Aaron分數:8.31
姓名:Aideen分數:8.05
姓名:Stacey分數:7.81
姓名:Zack分數:7.12
>>>
資料變複雜了:
line="101;Johnny 'wave-boy' Jones;USA;8.65;Fish;21"
s={}
(s['id'],s['name'],s['country'],s['average'],s['board'],s['age'])=line.split(";")
print("ID: "+s['id'])
print("Name: "+s['name'])
print("Country: "+s['country'])
print("Average: "+s['average'])
print("Board type: "+s['board'])
print("Age: "+s['age'])
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
ID: 101
Name: Johnny 'wave-boy' Jones
Country: USA
Average: 8.65
Board type: Fish
Age: 21
>>>
進一步改進:
def find_details(id2find):
surfers_f=open("surfing_data.csv")
for each_line in surfers_f:
s={}
(s['id'],s['name'],s['country'],s['average'],s['board'],s['age'])=each_line.split(";")
if id2find==int(s['id']):
surfers_f.close()
return(s)
surfers_f.close()
return(s)
lookup_id=int(input("Enter the id of the surrfer:"))
surfer=find_details(lookup_id)
if surfer:
print("ID: "+surfer['id'])
print("Name: "+surfer['name'])
print("Country: "+surfer['country'])
print("Average: "+surfer['average'])
print("Board type: "+surfer['board'])
print("Age: "+surfer['age'])
運作結果
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Enter the id of the surrfer:101
ID: 101
Name: Johnny 'wave-boy' Jones
Country: USA
Average: 8.32
Board type: Fish
Age: 21
>>> 202
202
>>>
資料庫
import sqlite3
def find_details(id2find):
db=sqlite3.connect("surfersDB.sdb")
db.row_factory=sqlite3.row
cursor=db.cursor()
cursor.execute("select * from surfers")
rows=cursor.fetchall()
for row in rows:
if row['id']==id2find:
s={}
s['id']=str(row['id'])
s['name']=row['name']
s['country']=row['country']
s['average']=str(row['average'])
s['board']=row['board']
s['age']=str(row['age'])
cursor.close()
return(s)
cursor.close()
return({})
lookup_id=int(input("Enter the id of the surrfer:"))
surfer=find_details(lookup_id)
if surfer:
print("ID: "+surfer['id'])
print("Name: "+surfer['name'])
print("Country: "+surfer['country'])
print("Average: "+surfer['average'])
print("Board type: "+surfer['board'])
print("Age: "+surfer['age'])
總結:
{}:一個空哈希
s.keys() 提供一個清單,包含名為“s”的哈希中所有的關鍵字。
s.itms() 提供一個清單,包含名為“s”的哈希中所有的關鍵字和值。
line.split(“,”)在每個逗号出現處分割包含在變量“line”中的字元串。
sorted() 一個内置函數,可以對很多資料結構排序。
數組:一個變量,有許多可以存放資料的有索引的空位。
連結清單:一個變量,資料在其中形成了一根鍊條,鍊條上的一個資料項指向另一個資料項,被指向的資料項又接着指向再下一個資料項,以此類推。
隊列:一個變量,允許資料從集合的一端進入,從另一端離開,支援先進先出的機制。
哈希:一個變量,含有兩列和(可能)多行的資料。
集合:一個變量,包含了,一個由一些獨立資料項組成的集合。
多元數組:一個變量,可以用一個多元矩陣來包含資料(但是經常使用的次元隻是2)