1 / 22

Head First Python: Ch 5. Comprehending Data: Work that Data !

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.

giolla
Télécharger la présentation

Head First Python: Ch 5. Comprehending Data: Work that Data !

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Head First Python: Ch 5. Comprehending Data: Work that Data ! Aug 30, 2013 Inhoe Lee

  2. 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

  3. Coach Kelly needs your help • Coach needs a quick way to know the top three fastest times for each athlete

  4. 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

  5. Coach Kelly needs your help • Represented by four lists in Python’s memory • Require you to sort it

  6. 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()

  7. Practice • In-place sorting • Copied sorting

  8. Sorting • Update the code • Coach_2.py

  9. Sorting • Result • Data values are now uniform • Periods, dashes, and colons • Need to fix data

  10. The trouble with time • Raw data • Read from file • After sorting

  11. Sanitize • Sanitizing function

  12. Sanitize • Coach_3.py

  13. Sanitize • Output • Sorted, uniformly formatted • But, duplicated code

  14. Comprehending lists • Transform one list into another

  15. Comprehending lists

  16. Comprehending lists • Coach_4.py

  17. List slicing • List slice • What about removing duplicates from list?

  18. Iterate to remove duplicates

  19. Iterate to remove duplicates • coach_5.py

  20. Remove duplicates with sets • Set removes duplicate automatically • Coach_6.py

  21. 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.

  22. 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

More Related