Bored, look at "Head first Python" to pass the time. Feel this book is very general, can be bored when the turn over. There are a lot of pages in each chapter, but they don't say anything.
First look at chapter fifth. Record the Knowledge Points:
F.ReadLine (): reads a row of data from a file
split (): You can split a string into a list by a given delimiter
Sort (): Sorts the data in the list from small to large, raw data is corrupted, incoming reverse = True can be descending
sorted (): The data in the list is sorted from small to large, there is space in the copy, the original data is still in, the incoming reverse = True can be descending. Note the use of the sort is different. You can sort the collection to return a list.
Collections are not sliced, but can be sorted.
That is , sorted (set (A)) [0:3] is correct
Set (sorted (A)) [0:3] error
>>> s ='Apple,a,day,keeps,the,doctor,away'>>> L = s.split (',')>>>l['Apple','a',' Day','keeps',' the','Doctor','away']
>>> d =[2,7,1,4,9,3]>>> d.sort ()>>> d[1, 2, 3, 4, 7, 9]
>>> d =[2,7,1,4,9,3]>>> d2 = sorted (d)>>> d2[1, 2, 3, 4, 7, 9]< /c6>>>> d[2, 7, 1, 4, 9, 3]
For four sets of input data, output a minimum of three data that is not duplicated for each set of data:
The inputs are obtained from four TXT respectively:
[' 2.01 ', ' 2.01 ', ' 2.22 ', ' 2.34 ', ' 2.34 ', ' 2.45 ', ' 3.01 ', ' 3.10 ', ' 3.21 ']
[' 2.11 ', ' 2.11 ', ' 2.23 ', ' 2.23 ', ' 2.59 ', ' 3.10 ', ' 3.10 ', ' 3.21 ', ' 3.21 ']
[' 2.22 ', ' 2.38 ', ' 2.49 ', ' 3.01 ', ' 3.01 ', ' 3.02 ', ' 3.02 ', ' 3.02 ', ' 3.22 ']
[' 2.18 ', ' 2.25 ', ' 2.39 ', ' 2.54 ', ' 2.55 ', ' 2.55 ', ' 2.55 ', ' 2.58 ', ' 2.58 ']
Code:
defsanitize (time_string):if '-' inchTime_string:splitter='-' elif ':' inchTime_string:splitter=':' Else: return(time_string) (mins,secs)=Time_string.split (Splitter)return(mins +'.'+secs) JAF= Open ('James.txt','R') James= Jaf.readline (). Strip (). Split (',') Juf= Open ('Julie.txt','R') Julie= Juf.readline (). Strip (). Split (',') MIF= Open ('Mikey.txt','R') Mikey= Mif.readline (). Strip (). Split (',') SAF= Open ('Sarah.txt','R') Sarah= Saf.readline (). Strip (). Split (',') James_correct= [Sanitize (c) forCinchJames]julie_correct= [Sanitize (c) forCinchJulie]mikey_correct= [Sanitize (c) forCinchMikey]sarah_correct= [Sanitize (c) forCinchSarah]Print(Sorted (set (James_correct)) [0:3])Print(Sorted (set (Julie_correct)) [0:3])Print(Sorted (set (Mikey_correct)) [0:3])Print(Sorted (set (Sarah_correct)) [0:3])
>>> ================================ RESTART ================================>>> ['2.01','2.22','2.34']['2.11','2.23','2.59']['2.22','2.38','2.49']['2.18','2.25','2.39']
"Python" Head first python (v)