Download presentation
Presentation is loading. Please wait.
Published byLindsay Garrett Modified over 8 years ago
1
Vocabulary Size of Moby Dick
2
Algorithm 1.Read the text into Python (as a huge string) 2.Split the string into words 3.Remove duplicates 4.Count the size
3
Remove Duplicates 1.Read the text into Python (as a huge string) 2.Split the string into words 3.Remove duplicates 1.Sort the word list 2.Create an empty vocab list, and put the first word in the word list into the vocab list 3.For each of the rest words in the word list 1.If it is the same as the last in the vocab list, do nothing 2.If it is different from the last in the voca list, add it to the vocab list 4.Count the size
4
Algorithm – Take 2 1.Read the text into Python (as a huge string) 2.Clean up the string 1.Remove punctuations 2.Remove numbers 3.Convert all letters to lowercase 3.Split the string into words 4.Remove duplicates 5.Count the size
5
Now Let's Get Even Fancier What about derived words? seem seems seemed seeming seemingly Removing them is almost like removing duplicates, except…
6
Searching Through A List Is slow. Proof: longList = range(1,20000) for i in range(1,20000): if 19999 in longList: pass It's slow because it's checking equality sequentially. So this happens much faster: longList = range(1,20000) for i in range(1,20000): if 1 in longList: pass Can we do better?
7
How do we look up a word in a Dictionary?
8
You Can Create A Dictionary In Python It provides Not only an thingy (a technical term), but also another thingy associated with it (e.g., a word (string) and its definition (string) Fast searching just like looking up words in a dictionary Working with dictionary is almost like working with list You can press tab to find out what you can do with a list or dictionary (or almost every other thing)
9
myList = [] myList += 'banana' myList = ['banana', 'mango'] myList.pop(0) myList[0] myList[0] = 'grape' myDic = {} myDic['banana'] = 5 myDic = {'banana':5, 'mango':5} myDic.pop('banana') myDic['mango'] myDic['mango'] = 10
10
Now Let's Get A Bit Technical You have seen a lot of thingies, name them (things that you can assign as a value to a variable) They are all objects (things that can be used in the name.function() way). Maybe except numbers… Lists and dictionaries, being objects themselves, are also data structures (i.e., they organize data). It's like boxes are used to organize objects, and they are objects themselves. Moreover, they can be organized using other boxes.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.