Hands-on with collections.defaultdict
Now that you have a basic understanding of what collections.defaultdict
is and how it can be used, let's dive into some hands-on examples to solidify your knowledge.
Example 1: Counting Word Frequencies
Suppose you have a text file containing a large amount of text, and you want to count the frequency of each word in the file. You can use a defaultdict
to make this task much easier.
from collections import defaultdict
## Open the text file
with open('text.txt', 'r') as file:
text = file.read().lower().split()
## Create a defaultdict to store word frequencies
word_freq = defaultdict(int)
## Count the frequency of each word
for word in text:
word_freq[word] += 1
## Print the top 10 most frequent words
top_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True)[:10]
for word, count in top_words:
print(f"{word}: {count}")
In this example, we first open a text file and read its contents. We then create a defaultdict
with a default factory function of int
to store the word frequencies. We loop through the words in the text and increment the count for each word in the defaultdict
. Finally, we sort the defaultdict
by the count values and print the top 10 most frequent words.
Example 2: Grouping Data by Multiple Keys
Suppose you have a list of tuples representing student information, and you want to group the students by their grade and class. You can use a nested defaultdict
to accomplish this task.
from collections import defaultdict
## Student information
students = [
('Alice', 'A', 'Math'),
('Bob', 'B', 'Math'),
('Charlie', 'A', 'English'),
('David', 'B', 'English'),
('Eve', 'A', 'Math'),
('Frank', 'B', 'English')
]
## Create a nested defaultdict to group students
student_groups = defaultdict(lambda: defaultdict(list))
## Group the students by grade and class
for name, grade, subject in students:
student_groups[grade][subject].append(name)
## Print the grouped student information
for grade, class_groups in student_groups.items():
print(f"Grade {grade}:")
for subject, student_names in class_groups.items():
print(f" {subject}: {', '.join(student_names)}")
In this example, we create a nested defaultdict
with a default factory function that returns another defaultdict
with a default factory function that returns a list. We then loop through the student information and add each student to the appropriate group based on their grade and subject. Finally, we print the grouped student information.
These examples should give you a good starting point for using collections.defaultdict
in your own Python projects. Remember, the key to effectively using defaultdict
is understanding the types of problems it can help solve and how to leverage its unique features to simplify your code and make it more efficient.