#!/usr/bin/env python # coding: utf-8 # # Dictionary # In[1]: my_dict = {} grades = {'Ana':'B', 'John':'A+', 'Denise':'A', 'Katy':'A'} # In[2]: grades['John'] # In[3]: grades['sylvan'] # In[4]: grades['Sylvan'] = 'A' # add an entry grades['Sylvan'] # In[5]: 'John' in grades # In[6]: 'Daniel' in grades # In[7]: del(grades['Ana']) # In[8]: grades # In[10]: grades.keys() # In[11]: grades.values() # In[12]: d = {4:{1:0}, (1,3):"twelve", 'const':[3.14,2.7,8.44]} d # ## Ví dụ - Analyze song lyrics # In[14]: def lyrics_to_frequencies(lyrics): myDict = {} for word in lyrics: if word in myDict: myDict[word] += 1 else: myDict[word] = 1 return myDict # In[16]: she_loves_you = ['she', 'loves', 'you', 'yeah', 'yeah', 'yeah', 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah', 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah', 'you', 'think', "you've", 'lost', 'your', 'love', 'well', 'i', 'saw', 'her', 'yesterday-yi-yay', "it's", 'you', "she's", 'thinking', 'of', 'and', 'she', 'told', 'me', 'what', 'to', 'say-yi-yay', 'she', 'says', 'she', 'loves', 'you', 'and', 'you', 'know', 'that', "can't", 'be', 'bad', 'yes', 'she', 'loves', 'you', 'and', 'you', 'know', 'you', 'should', 'be', 'glad', 'she', 'said', 'you', 'hurt', 'her', 'so', 'she', 'almost', 'lost', 'her', 'mind', 'and', 'now', 'she', 'says', 'she', 'knows', "you're", 'not', 'the', 'hurting', 'kind', 'she', 'says', 'she', 'loves', 'you', 'and', 'you', 'know', 'that', "can't", 'be', 'bad', 'yes', 'she', 'loves', 'you', 'and', 'you', 'know', 'you', 'should', 'be', 'glad', 'oo', 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah', 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah', 'with', 'a', 'love', 'like', 'that', 'you', 'know', 'you', 'should', 'be', 'glad', 'you', 'know', "it's", 'up', 'to', 'you', 'i', 'think', "it's", 'only', 'fair', 'pride', 'can', 'hurt', 'you', 'too', 'pologize', 'to', 'her', 'Because', 'she', 'loves', 'you', 'and', 'you', 'know', 'that', "can't", 'be', 'bad', 'Yes', 'she', 'loves', 'you', 'and', 'you', 'know', 'you', 'should', 'be', 'glad', 'oo', 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah', 'she', 'loves', 'you', 'yeah', 'yeah', 'yeah', 'with', 'a', 'love', 'like', 'that', 'you', 'know', 'you', 'should', 'be', 'glad', 'with', 'a', 'love', 'like', 'that', 'you', 'know', 'you', 'should', 'be', 'glad', 'with', 'a', 'love', 'like', 'that', 'you', 'know', 'you', 'should', 'be', 'glad', 'yeah', 'yeah', 'yeah', 'yeah', 'yeah', 'yeah', 'yeah' ] beatles = lyrics_to_frequencies(she_loves_you) beatles # In[18]: def most_common_words(freqs): values = freqs.values() best = max(freqs.values()) words = [] for k in freqs: if freqs[k] == best: words.append(k) return (words, best) (w, b) = most_common_words(beatles) w # In[20]: def words_often(freqs, minTimes): result = [] done = False while not done: temp = most_common_words(freqs) if temp[1] >= minTimes: result.append(temp) for w in temp[0]: del(freqs[w]) #remove word from dictionary else: done = True return result # In[21]: print(words_often(beatles, 5)) # ## Ví dụ - Fibonacci # In[25]: def fib_efficient(n, d): if n in d: return d[n] else: ans = fib_efficient(n-1, d) + fib_efficient(n-2, d) d[n] = ans return ans d = {1:1, 2:2} print(fib_efficient(33, d)) # In[24]: def fib(x): if x == 0 or x == 1: return 1 else: return fib(x - 1) + fib(x - 2) print(fib(33)) # ## Biến toàn cục - Global variables # In[27]: def fib(n): global numFibCalls numFibCalls += 1 if n == 1 or n == 2: return n else: return fib(n-1) + fib(n-2) def fibef(n, d): global numFibCalls numFibCalls += 1 if n in d: return d[n] else: ans = fibef(n-1, d) + fibef(n-2, d) d[n] = ans return ans numFibCalls = 0 fibArg = 34 print(fib(fibArg)) print('function calls', numFibCalls) numFibCalls = 0 d = {1:1, 2:2} print(fibef(fibArg, d)) print('function calls', numFibCalls) # - - - # [Trước: Tuple và List](http://nbviewer.jupyter.org/github/manleviet/introCSusingPython/blob/master/5%20-%20Tuple%2C%20List.ipynb) | # [Mục lục](http://nbviewer.jupyter.org/github/manleviet/introCSusingPython/blob/master/index.ipynb) | # [Tiếp: Kiểm thử chương trình](http://nbviewer.jupyter.org/github/manleviet/introCSusingPython/blob/master/7%20-%20Ki%E1%BB%83m%20th%E1%BB%AD%20ch%C6%B0%C6%A1ng%20tr%C3%ACnh.ipynb)