Scores={}result_f=open ("Results.txt") for line in Result_f: (Name,score) =line.split () scores[score]= Nameresult_f.close () print ("The top scores were:") for Each_score in Scores.keys (): print (' name: ' +scores[each_score] + ' score: ' +each_score)
To add a sort method:
Scores={}result_f=open ("Results.txt") for line in Result_f: (Name,score) =line.split () scores[score]= Nameresult_f.close () print ("The top scores were:") for Each_score in sorted (Scores.keys (), reverse=true): print (' Name: ' +scores[each_score]+ ' score: ' +each_score '
The data becomes complex:
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, 22:43:06) [MSC v.1600 + bit (Intel)] on Win32type "copyright", "credits" or "license ()" For more information.>>> ================================ RESTART =========================== =====>>> ID: 101Name: Johnny ' Wave-boy ' jonescountry: usaaverage: 8.65Board Type: Fishage:
Further improvements:
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 ')
Run results
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb, 22:43:06) [MSC v.1600 + bit (Intel)] on Win32type "copyright", "credits" or "license ()" For more information.>>> ================================ RESTART =========================== =====>>> Enter The ID of the surrfer:101id: 101Name: Johnny ' Wave-boy ' jonescountry: usaaverage:< C3/>8.32board type: fishage:
Database
Import Sqlite3def find_details (id2find): db=sqlite3.connect ("Surfersdb.sdb") Db.row_factory=sqlite3.row cursor=db.cursor () cursor.execute ("SELECT * from surfers") Rows=cursor.fetchall () as 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 ')
Summarize:
{}: An empty hash
S.keys () provides a list that contains all the keywords in the hash named "s".
S.ITMS () provides a list that contains all the keywords and values in the hash named "s".
Line.split (",") splits the string contained in the variable "line" at each comma occurrence.
Sorted () A built-in function that can sort many data structures.
Array: A variable that has a number of indexed slots that can hold data.
Linked list: A variable in which the data forms a chain, a data item on the chain points to another data item, the pointed data item then points to the next data item, and so on.
Queue: A variable that allows data to enter from one end of the collection, leaving from the other, and supporting a FIFO mechanism.
Hash: A variable that contains two columns and (possibly) multiple rows of data.
Collection: A variable that contains a collection of independent data items.
Multidimensional arrays: A variable that can contain data with a multidimensional matrix (but the most frequently used dimension is only 2)
Hashes and databases