标签:
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)
标签:
原文地址:http://www.cnblogs.com/lizanqirxx/p/5153435.html