Recently in the study of Python, the use of the process has a list and dict declaration is very special, under this note
The dictionary is a mutable object, the initialization must not be placed in front of the for loop before the list declaration must be placed at the beginning of the program (the list is also mutable object, but do not know why it must be declared at the beginning) code if names=[] is placed inside the for loop, its names.append () After that, print names always prints only the last value, and the Declaration of dic={} in the following code is initially placed in front of the For loop, which also causes the dictionary table to print for the last time the following code is the subject of the practice Vamei teacher, Returns the name of a person with a score less than 60 in the text Record.txt content as follows:
# name, age, score 19,56,.
I finished my own research and wrote this paragraph:
Def readfiles (): l=[] names=[] f=open ("E:\\record.txt", "R") for line in F: dic={} if line[0 ]== "#": li0=line[1:-1].split (",") #print li0 Else: li1=line[0:-1].split (",") #print Li1 dic["name"]=li1[0] dic["age"]=li1[1] dic["score"]=li1[2] l.append (DIC) print L F.close () for I in range (len (L)): #print l[i]["score"] #names =[] if l[i]["score"]< "60": names.append (l[i]["name"]) #print names print names
Vamei Teacher's tutorial is good, writing simple and clear, suitable for beginners to get started. Http://www.cnblogs.com/vamei/tag/Python/default.html?page=3
Python Learning Notes