Download presentation
Presentation is loading. Please wait.
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 ... >>> word_list = 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
Try file_ops.py >>> f = open( ‘greeting.txt‘, ‘w’ )
>>> text = “Hello, world!” >>> f.write(text) >>> f.close() opens the file for writing and calls it f Writes string text to f Try file_ops.py
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 Try wc.py
5
Dictionaries In Python a dictionary is a set of key - value pairs.
1999 Feb 16 2000 Feb 4 Rabbit 1998 Jan 28 1999 Feb 15 Tiger 1997 Feb 7 1998 Jan 27 Ox 1996 Feb 19 1997 Feb 6 Rat Dictionaries In Python a dictionary is a set of key - value pairs. >>> d = {} >>> d[1996] = ‘Rat' >>> d[1997] = ‘Ox' >>> d {1996:‘Rat', 1997:‘Ox'} >>> d[1996] ‘Rat' >>> d[1969] key error creates an empty dictionary, d 1996 is the key ‘Rat' is the value 1997 is the key ‘Ox' is the value Curly! And colony! Retrieve data like lists… It's a sequence where the index can be any immutable key.
6
More on dictionaries Dictionaries have lots of built-in methods, or functions: >>> d = {1996:‘Rat', 1997:‘Ox'} >>> 1996 in d >>> 1969 in d True False >>> len(d) 2 >>> list(d.keys()) [ 1996, 1997 ] >>> list(d.values()) [ ‘Rat', ‘Ox' ] >>> list(d.items()) [ (1996, ‘Rat'), (1997, ‘Ok') ] 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
7
""" 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 wc( 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.
8
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?
9
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
10
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 }
11
A dictionary challenge…
state_dict = { 'AL': 0, ‘AK’: 0,… } All US state abbreviations are in this dictionary… key value def state_challenge( state_dict ): while 0 in state_dict.values(): print(list(state_dict.values()).count(0), \ "states left to guess") guess = input("Name a state: ") if guess not in state_dict: print 'Try again...' elif state_dict[guess] == 0: print 'Yes!' state_dict[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
12
File exercise
13
Write a function remove_duplicates(somelist) that removes duplicates from the list. Example, alist = [‘hello’, ‘world’, ‘hello’] >>> remove_duplicates(alist) [‘hello’, ‘world’] >>> alist = [‘how’, ‘are’, ‘you’] >>> remove_duplicates[alist) [‘how’, ‘are’, ‘you’]
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.