230 likes | 404 Views
Head First Python: Ch 5. Comprehending Data: Work that Data !. Aug 30, 2013 Inhoe Lee. Outline. Coach Kelly needs your help Sort in one of two ways The trouble with time Comprehending lists Iterate to remove duplicates Remove duplicates with sets Your Python Toolbox.
E N D
Head First Python: Ch 5. Comprehending Data: Work that Data ! Aug 30, 2013 Inhoe Lee
Outline • Coach Kelly needs your help • Sort in one of two ways • The trouble with time • Comprehending lists • Iterate to remove duplicates • Remove duplicates with sets • Your Python Toolbox
Coach Kelly needs your help • Coach needs a quick way to know the top three fastest times for each athlete
What you need to do • Open the file • Read the line of data • Convert the data to a list • Display the four lists on screen • Coach_1.py
Coach Kelly needs your help • Represented by four lists in Python’s memory • Require you to sort it
Sort in one of two ways • In-place sorting • Replace original data • Original ordering is lost • sort() • Copied sorting • Return a sorted copy of original data • Original data’s ordering is maintained • sorted()
Practice • In-place sorting • Copied sorting
Sorting • Update the code • Coach_2.py
Sorting • Result • Data values are now uniform • Periods, dashes, and colons • Need to fix data
The trouble with time • Raw data • Read from file • After sorting
Sanitize • Sanitizing function
Sanitize • Coach_3.py
Sanitize • Output • Sorted, uniformly formatted • But, duplicated code
Comprehending lists • Transform one list into another
Comprehending lists • Coach_4.py
List slicing • List slice • What about removing duplicates from list?
Iterate to remove duplicates • coach_5.py
Remove duplicates with sets • Set removes duplicate automatically • Coach_6.py
Factory Function • Factory Function • make new data items of a particular type. • For instance, “set()” • In the real world, factories make things, hence the name.
Your Python Toolbox • Sort() • In-place sorting • Sorted() • Copied sorting • Reverse=True • Arrange data in descending order • My_list[3:6] • Access from location3 up-to-but-not-including location6 • Set() • Create a set