Counter
Resources
- Official collections module documentation.
- Real Python tutorial on Counter.
- Real Python introduction to defaultdict
collections module
The standard library collections module holds some little useful tools like: Counter, OrderedDict or defaultdict.
Counter
Counter is the pythonic way of counting items.
Let’s simulate that we throw a dice 1000 times and then we count the number of times that we get each number.
Exercises
Create a Counter to count how many times each nucleotide appears in a DNA sequence.
Given a list of words, count how many end in “ing” and how many do not.
Count the words in a text.
Count the words in “El Quijote”, you can get the text file from the Gutenberg project. To do it in just one line you might find useful to use nested generator expressions. It might also be useful to have a list with the punctuation signs that you can find in the string module.
Which are the 10 most common words?
Using zip and Counter count the number of matches and mismatches in two aligned DNA sequences.