Presentation is loading. Please wait.

Presentation is loading. Please wait.

Files and Dictionaries

Similar presentations


Presentation on theme: "Files and Dictionaries"— Presentation transcript:

1 Files and Dictionaries

2 Files >>> f = open( ‘spam.txt‘, ‘r’ )
In Python reading files is no problem… >>> f = open( ‘spam.txt‘, ‘r’ ) >>> text = f.read() alternative… >>> text = f.read(50) >>> f.close() >>> text 'I like poptarts and 42 and spam.\nWill I ... >>> wordList = text.split() [ 'I', 'like', 'poptarts', ... ] opens the file for reading and calls it f reads the whole file and calls it f reads the next 50 bytes from the file Point out that open is a method on file objects (technically, TextIOStream objects, but stay away from that complexity). And split is a method on string objects. Might want to demonstrate this on the spam.txt file. closes the file (optional) all the text (as one big string) text.split() returns a list of each "word"

3 >>> f = open( ‘spam.txt‘, ‘w’ )
>>> text = “Hello, world!” >>> f.write(text) >>> f.close() opens the file for writing and calls it f Writes string text to f

4 Count how many words are in a given file
def wc( filename ): """ word-counting program """ f = open( filename, “r”) text = f.read() f.close() words = text.split() print("There are", len(words), "words.") file handling word counting This is in cha6_2.py, but it is the complete function, so you might want to go to the next slide first

5 Write a function removeDuplicates(somelist) that removes duplicates from the list. Example, alist = [‘hello’, ‘world’, ‘hello’] >>> removeDuplicates(alist) [‘hello’, ‘world’] >>> alist = [‘how’, ‘are’, ‘you’] >>> removeDuplicates[alist) [‘how’, ‘are’, ‘you’]

6 Dictionaries In Python a dictionary is a set of key - value pairs.
>>> d[1992] = 'monkey' >>> d[1993] = 'rooster' >>> d {1992:'monkey', 1993:'rooster'} >>> d[1992] 'monkey' >>> d[1969] key error creates an empty dictionary, d 1992 is the key 'monkey' is the value 1993 is the key 'rooster' is the value Curly! And colony! Retrieve data like lists… It's a sequence where the index can be any immutable key.

7 More on dictionaries Dictionaries have lots of built-in methods, or functions: >>> d = {1992:'monkey', 1993:'rooster'} >>> 1992 in d >>> 1969 in d True False >>> len(d) 2 >>> list(d.keys()) [ 1992, 1993 ] >>> list(d.values()) [ 'monkey', 'rooster' ] >>> list(d.items()) [ (1992, 'monkey'), (1993, 'rooster') ] in checks if a key is present len returns the # of keys d.keys returns a list of all keys d.values returns a list of all keys d.items returns a list of all key, value pairs

8 """ word-counting program """ f = open( filename ) text = f.read()
What if we wanted the number of different words in the file? This would be the author's vocabulary size, instead of the total word count. def vc( filename ): """ word-counting program """ f = open( filename ) text = f.read() f.close() words = text.split() print("There are", len(words), "words.") d = {} for w in words: if w not in d: d[w] = 1 else: d[w] += 1 print ("There are", len(d), "distinct words.\n“) return len(d) # return the number of distict words file handling word counting Tracking the number of occurences of each word with a dictionary, d. Demo wc() using spam.txt.

9 Vocabularists? Shakespeare used 31,534 different words and a grand total of 884,647 words counting repetitions (across his works) Active vocabulary estimates range from 10,000-60,000. Passive vocabulary estimates are much higher. Many Shakespearean contributions: One contemporary author in the Oxford Eng. Dictionary… Shakespeare which word?

10 Vocabularists? Shakespeare used 31,534 different words and an estimated vocabulary of 66,534 words (across his works) Average English speaker knows 10,000 to 20,000 words. Many Shakespearean contributions: Shakespeare J. K. Rowling

11 Consider a game of guessing all 50 states…
A state challenge… Consider a game of guessing all 50 states… STATES = { 'AL':0, 'AK':0, 'AZ':0, 'AR':0, 'CA':0, 'CO':0, 'CT':0, 'DE':0, 'DC':0, 'FL':0, 'GA':0, 'HI':0, 'ID':0, 'IL':0, 'IN':0, 'IA':0, 'KS':0, 'KY':0, 'LA':0, 'ME':0, 'MD':0, 'MA':0, 'MI':0, 'MN':0, 'MS':0, 'MO':0, 'MT':0, 'NE':0, 'NV':0, 'NH':0, 'NJ':0, 'NM':0, 'NY':0, 'NC':0, 'ND':0, 'OH':0, 'OK':0, 'OR':0, 'PA':0, 'RI':0, 'SC':0, 'SD':0, 'TN':0, 'TX':0, 'UT':0, 'VT':0, 'VA':0, 'WA':0, 'WV':0, 'WI':0, 'WY':0 }

12 A dictionary challenge…
stateDict = { 'AL': 0, … } All US state abbreviations are in this dictionary… key value def stateChallenge( stateDict ): while 0 in stateDict.values(): print(list(stateDict.values()).count(0), \ "states left to guess") guess = input("Name a state: ") if guess not in stateDict: print 'Try again...' elif stateDict[guess] == 0: print 'Yes!' stateDict[guess] += 1 else: print('Already guessed...') print('Phew!') Tell user how many left to guess wasn't a key at all Run stateChallenge in the script… newly guessed key: value was 0 repeated guess


Download ppt "Files and Dictionaries"

Similar presentations


Ads by Google