-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBuilding Dictionaries.py
51 lines (33 loc) · 1.28 KB
/
Building Dictionaries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python
# coding: utf-8
# ## 1) Using a for loop to create a set of counters.
# ### Keeping track of the total count of each word.
# In[14]:
# Create a list of book titles
book_title = ['great', 'expectaions', 'the', 'of', 'the']
# Initialize an empty dictionary to count word occurrences
word_counter = {}
# Iterate through each word in the book_title list
for word in book_title:
# If the word is not already a key in word_counter, add it with an initial count of 1
if word not in word_counter:
word_counter[word] = 1
else:
# If the word is already a key, increment its count by 1
word_counter[word] += 1
# Print the word_counter dictionary
print(word_counter)
# ## 2) Using get method
# ### Keeping track of the total count of each word.
# In[16]:
# Create a list of book titles
book_title = ['great', 'expectaions', 'the', 'of', 'the']
# Initialize an empty dictionary to count word occurrences
word_counter = {}
# Iterate through each word in the book_title list
for word in book_title:
# Use the get() method to retrieve the current count for the word
# If the word is not already a key in word_counter, default to 0
word_counter[word] = word_counter.get(word, 0) + 1
# Print the word_counter dictionary
print(word_counter)