1 of 24

ME 4990: Intro to CS�Object-Oriented Programming & Machine Learning 101

Dictionary and set

2 of 24

Outline

  • Dictionary
    • Basic
    • Histogram
    • Practice
  • Set
    • Basic
    • Practice

3 of 24

Dictionary

  • A dictionary is like a list, but more general.
  • In a list, the indices have to be integers; in a dictionary they can be (almost) any type.

4 of 24

Dictionary represent mapping

  • Dictionary is a mapping from key to values
  • In the example: key is ‘one’, value is ‘uno’

5 of 24

Dictionary

  • The method to initialize a dictionary varied, one method is:

  • You can find the value of any existing key

    • What do we get for key “four”?

6 of 24

Dictionary

  • Some handy tips
  • Use len() to find the length of a dictionary:

  • Use keys() and values() method to acquire all keys and values

7 of 24

Dictionary

  • Search by key use in

  • Search by values:

8 of 24

Dictionary

  • Does dictionary have a key?

  • Usually, an dictionary is unordered.
    • In C++, it is called “unordered_map

9 of 24

Dictionary

  • You can transverse a dictionary in order

10 of 24

Dictionary Reverse look up

  • Reverse lookup is not common

11 of 24

Can Index be anything?

  • The key must be “hashable”
  • How about we set the keys to be a list?

  • A hash is a function that takes a value (of any kind) and returns an integer

12 of 24

Histogram

  • It is common to use a dictionary for histogram
  • E.g. count the number of each letter in a given word by ChatGPT

13 of 24

Inverse a dictionary

  • We can inverse a histogram

14 of 24

Practice 1: Histogram

  • Solution: see compare_word.py
  • We have two strings:
    • a= “Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch”
    • b=“obwllllantysiliogogogochLlanfairpwllgwyngyllgogerychwyrndr”
  • Do they contain the same letter?

  • Does each letter repeat the same time?

15 of 24

Histogram practice

  • We can use histogram

16 of 24

Practice 2: Histogram

  • Write a function called has_duplicates that takes a list and returns True if there is any element that appears more than once. It should not modify the original list.
  • Solution (by ChatGPT): see has_duplicates.py

  • Drawback of the solution?

17 of 24

Practice 2: Histogram

18 of 24

Outline

  • Dictionary
    • Basic
    • Histogram
    • Practice
  • Set
    • Basic
    • Practice

19 of 24

Set

  • A set can be perceived as a dictionary with only keys

  • Compare to dictionary:

20 of 24

Find in set

  • You can find in set:

21 of 24

Add in a set

  • We can add a key in a set

22 of 24

Remove from a set

  • There are many ways with tiny differences:

https://www.w3schools.com/python/python_sets_remove.asp

23 of 24

Change a key?

  • If we want to change orange to avocado?

  • Just remove and add, please

24 of 24

Practice: set

  • Write a function called has_duplicates that takes a list and returns True if there is any element that appears more than once. It should not modify the original list.
  • Use set is easier
  • Solution: see has_duplicate_set.py