Last active
February 13, 2025 23:54
-
-
Save LizaPiya/1ec95bcf5c70893a9cb6c974458c8b8f to your computer and use it in GitHub Desktop.
Introduction: Accumulating Multiple Results In a Dictionary
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## If we want to find out how often the letter ‘t’ occurs, we can accumulate the result in a count variable. | |
| f = open('scarlet.txt', 'r') | |
| txt = f.read() | |
| # now txt is one long string containing all the characters | |
| t_count = 0 #initialize the accumulator variable | |
| for c in txt: | |
| if c == 't': | |
| t_count = t_count + 1 #increment the counter | |
| print("t: " + str(t_count) + " occurrences") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## We can accumulate counts for more than one character as we traverse the text. Suppose, for example, we wanted to compare the counts of ‘t’ and ‘s’ in the text. | |
| f = open('scarlet.txt', 'r') | |
| txt = f.read() | |
| # now txt is one long string containing all the characters | |
| t_count = 0 #initialize the accumulator variable | |
| s_count = 0 # initialize the s counter accumulator as well | |
| for c in txt: | |
| if c == 't': | |
| t_count = t_count + 1 #increment the t counter | |
| elif c == 's': | |
| s_count = s_count + 1 | |
| print("t: " + str(t_count) + " occurrences") | |
| print("s: " + str(s_count) + " occurrences") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| f = open('scarlet.txt', 'r') | |
| txt = f.read() | |
| # now txt is one long string containing all the characters | |
| letter_counts = {} # start with an empty dictionary | |
| for c in txt: | |
| if c not in letter_counts: | |
| # we have not seen this character before, so initialize a counter for it | |
| letter_counts[c] = 0 | |
| #whether we've seen it before or not, increment its counter | |
| letter_counts[c] = letter_counts[c] + 1 | |
| for c in letter_counts.keys(): | |
| print(c + ": " + str(letter_counts[c]) + " occurrences") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ## Provided is a string saved to the variable name sentence. Split the string into a list of words, then create a dictionary that contains each word and the number of times it occurs. Save this dictionary to the variable name word_counts. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment