"Python" Head first python (v)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.