1 / 27

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING. Jehan-François Pâris jfparis@uh.edu Fall 2016. THE ONLINE BOOK CHAPTER XII DICTIONARIES. Dictionaries (I). Store pair s of entries called items { 'CS' : '743-713-3350', 'UHPD' : '713-743-3333'} Each pair of entries contains A key A value

bethv
Download Presentation

COSC 1306 COMPUTER SCIENCE AND PROGRAMMING

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. COSC 1306COMPUTER SCIENCE AND PROGRAMMING Jehan-François Pâris jfparis@uh.edu Fall 2016

  2. THE ONLINE BOOKCHAPTER XIIDICTIONARIES

  3. Dictionaries (I) Storepairs of entries called items{ 'CS' : '743-713-3350', 'UHPD' : '713-743-3333'} Each pair of entries contains A key A value Key and values are separated by a colon Pairs of entries are separated by commas Dictionary is enclosed within curlybraces

  4. Dictionaries (I) Storepairs of entries called items{ 'CS' : '743-713-3350', 'UHPD' : '713-743-3333'} Each pair of entries contains A key A value Key and values are separated by a colon Pairs of entries are separated by commas Dictionary is enclosed within curly braces

  5. Usage Keys must be unique within a dictionary No duplicates If we haveage = {'Alice' : 25, 'Bob' :28}thenage['Alice'] is25andage['Bob'] is 28

  6. Dictionaries are mutable >>> age = {'Alice' : 25, 'Bob' : 28} >>> saved = age >>> age['Bob'] = 29 >>> age{'Bob': 29, 'Alice': 25} >>> saved{'Bob': 29, 'Alice': 25}

  7. Keys must be unique >>> age = {'Alice' : 25, 'Bob' : 28, 'Alice' : 26} >>> age{'Bob': 28, 'Alice': 26}

  8. Adding contents age = {} age['Alice'] = 25 • Cannot do the same with lists • alist=[] • alist[10]= 5 # NO NO

  9. Displaying contents (I) >>> age = {'Alice' : 25, 'Carol': 'twenty-two'} >>> age.items()dict_items([ ('Alice', 25), ('Carol', 'twenty-two')]) >>> age.keys()dict_keys([ 'Alice', 'Carol']) age.values()dict_values([25, 'twenty-two'])

  10. Displaying items (II) • To represent mydictionary as a list of KV tuples • mydictionary.items() • To get the keys • mydictionary.keys() • To get the values • mydictionary.values() • Cannot index any of these three representations

  11. Updating dictionaries >>> age = {'Alice': 26 , 'Carol' : 22} >>> age.update({'Bob' : 29})>>> age{'Bob': 29, 'Carol': 22, 'Alice': 26} >>> age.update({'Carol' : 23})>>> age{'Bob': 29, 'Carol': 23, 'Alice': 26} >>> age['Bob'] = 30>>> age{'Bob': 30, 'Carol': 23, 'Alice': 26}

  12. Returning a value >>> age = {'Bob': 29,'Liz': 23,'Ann': 26} >>> age.get('Bob')29 >>> age['Bob']29

  13. Removing a specific item (I) >>> a = {'Ann' : 24, 'Liz' : 'twenty-two'} >>> a{'Liz': 'twenty-two', 'Ann': 24} >>> a.pop('Liz')'twenty-two' >>> a{'Ann': 24}

  14. Removing a specific item (II) >>> a.pop('Alice')26 >>> a{} >>>

  15. Remove a random item >>> age = {'Bob': 29, 'Liz': 22, 'Ann': 27} >>> age.popitem()('Bob', 29) >>> age {'Liz': 22, 'Ann': 27} >>> age.popitem()('Liz', 23) >>> age{'Ann': 27}

  16. TUPLES NOT COVERED

  17. Usage Keys must be unique within a dictionary No duplicates If we haveage = {'Alice' : 25, 'Bob' :28}then age['Alice'] is 25and age[‘Bob'] is 28

  18. Dictionaries are mutable >>> age = {'Alice' : 25, 'Bob' : 28} >>> saved = age >>> age['Bob'] = 29 >>> age{'Bob': 29, 'Alice': 25} >>> saved{'Bob': 29, 'Alice': 25}

  19. Keys must be unique >>> age = {'Alice' : 25, 'Bob' : 28, 'Alice' : 26} >>> age{'Bob': 28, 'Alice': 26}

  20. Adding contents • age = {} • age['Alice'] = 25

  21. Displaying contents >>> age = {'Alice' : 25, 'Carol': 'twenty-two'} >>> age.items()dict_items([ ('Alice', 25), ('Carol', 'twenty-two')]) >>> age.keys()dict_keys([ 'Alice', 'Carol']) age.values()dict_values([25, 'twenty-two'])

  22. Updating dictionaries >>> age = {'Alice': 26 , 'Carol' : 22} >>> age.update({'Bob' : 29})>>> age{'Bob': 29, 'Carol': 22, 'Alice': 26} >>> age.update({'Carol' : 23})>>> age{'Bob': 29, 'Carol': 23, 'Alice': 26} >>> age['Bob'] = 30>>> age{'Bob': 30, 'Carol': 23, 'Alice': 26}

  23. Returning a value >>> age = {'Bob': 29, 'Carol': 23, 'Alice': 26} >>> age.get('Bob')29 >>> age['Bob']29

  24. Removing a specific item (I) >>> a = {'Alice' : 26, 'Carol' : 'twenty-two'} >>> a{'Carol': 'twenty-two', 'Alice': 26} >>> a.pop('Carol’)'twenty-two' >>> a{'Alice': 26}

  25. Removing a specific item (II) >>> a.pop('Alice')26 >>> a{} >>>

  26. Remove a random item >>> age = {'Bob': 29, 'Carol': 23, 'Alice': 26} >>> age.popitem()('Bob', 29) >>> age {'Carol': 23, 'Alice': 26} >>> age.popitem()('Carol', 23) >>> age{'Alice': 26}

  27. TUPLES NOT COVERED

More Related