#!/usr/bin/env python # coding: utf-8 # # Dictionaries # - another compound type/container like lists and tuples # - very useful data structure/container that can store data as lookup table # - Python's mapping type similar to `map` container, or associative arrays in C++ and other languages # - dict maps keys (immutable type) to values of any type (heterogenous) # - Python uses complex hash algorithm to index key for fast access (Worst case "Big-Oh": $O(1)$), as a result the ordering is not maintained # ## creating dictionary objects # In[3]: eng2sp = {} # or eng2sp1 = dict() # In[2]: print(eng2sp, eng2sp1) # In[21]: eng2sp["One"] = "uno" eng2sp["two"] = "dos" eng2sp["three"] = "tres" eng2sp[4] = "quatro" eng2sp["five"] = "sinco" # In[ ]: key = 'Five' eng2sp[key] = 'Sinco' # In[4]: print(eng2sp) # In[7]: print(eng2sp['One']) # In[8]: symbolNames = {'*':'asterick', '+':"plus", '-': 'minus'} # In[9]: print(eng2sp, symbolNames) # In[10]: dict1 ={'one': 'uno', 'two': 'dos', 'three': 'tres', '4': 'quatro', 'five': 'sinco'} # ## accessing values # - use index operator ['key'] # - dict is mutable # In[12]: one = 'One' # In[13]: eng2sp[one] # In[18]: eng2sp['ten'] = 'diez' print(eng2sp['ten']) # In[25]: eng2sp['One'] = 'Uno' # In[30]: eng2sp['One'] = ['uno'] # In[33]: eng2sp['One'].append('Uno') # In[34]: print(eng2sp) # In[35]: adict = {1: ['uno', 'one'], 2:('two', 'dos'), 3:{'three':'tres'}} # In[37]: # How do you access tres in adict? print(adict[1][1]) print(adict[3]['three']) # ## dictionary methods # In[1]: help(dict) # In[5]: for k in eng2sp.keys(): # the order of the keys is not defined print("key {} maps to value {}".format(k, eng2sp[k])) # In[6]: # iterate over keys for k in eng2sp: print('key = {} value = {}'.format(k, eng2sp[k])) # In[7]: # iterate over values for val in eng2sp.values(): print("value = ", val) # In[8]: values = list(eng2sp.values()) # In[9]: values # In[10]: items = list(eng2sp.items()) # In[11]: print(items) # In[12]: dict2 = dict(items) print(dict2) # In[13]: for k, v in eng2sp.items(): print('{} -> {}'.format(k, v)) # In[19]: print(eng2sp.popitem()) print(eng2sp) # ## checking if key exists # - in and not in operators can be used to check for keys # In[23]: "One" in eng2sp # In[25]: "Ten" in eng2sp # In[26]: "twenty" not in eng2sp # # copying dictionary objects # - shallow copy vs deep copy # In[ ]: import copy digits = {1: 'one', 2: 'two', 3: ['three', 'Three', 'THREE']} digits1 = digits # creates an alias digits2 = digits.copy() # shallow copy digits3 = copy.deepcopy(digits) # deep copy # ### visualize in pythontutor.com: # - https://goo.gl/XmvYkB # In[8]: from IPython.display import IFrame src = ''' http://pythontutor.com/iframe-embed.html#code=import%20copy%0Adigits%20%3D%20%7B1%3A%20'one',%202%3A%20'two',%203%3A%20%5B'three',%20'Three',%20'THREE'%5D%7D%0Adigits1%20%3D%20digits%20%23%20creates%20an%20alias%0Adigits2%20%3D%20digits.copy%28%29%20%23%20shallow%20copy%0Adigits3%20%3D%20copy.deepcopy%28digits%29%20%23%20deep%20copy&codeDivHeight=400&codeDivWidth=350&cumulative=false&curInstr=0&heapPrimitives=false&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false ''' IFrame(src, width=900, height=600) # ## passing dict to function # - dict is a mutable type # - dict objects are passed by reference # In[3]: # find the histogram (frequency of each unique character) in a word def histogram(word, hist): for c in word: c = c.lower() if c in hist: hist[c] += 1 else: hist[c] = 1 # In[4]: h = {} histogram('Mississippim', h) for k, v in h.items(): print(k, v) # ## returning dict from functions # - dict objects can be returned from functions # In[6]: def getHist(word):# = "Mississippi" h = {} for c in word: if c in h: h[c] += 1 else: h[c] = 1 return h # In[7]: hist = getHist('Mississippi') print(hist) if 'M' in hist: print('M is in histogram') # ## Exercises # ### exercise 1 # Count and print letter frequency in some words. Hint: use get method # # ### exercise 2 # Write a program that reads a string and returns a table of the letters of the alphabet in alphabetical order which occur in the string together with the number of times each letter occurs. Case should be ignored. A sample output of the program when the user enters the data “ThiS is String with Upper and lower case Letters”, would look this: #
# a 2 # c 1 # d 1 # e 5 # g 1 # h 2 # i 4 # l 2 # n 2 # o 1 # p 2 # r 4 # s 5 # t 5 # u 1 # w 2 ## In[ ]: