Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access contents of list after applying Counter from collections module

I've applied the Counter function from the collections module to a list. After I do this, I'm not exactly clear as to what the contents of the new data structure would be characterised as. I'm also not sure what the preferred method for accessing the elements is.

I've done something like:

theList = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
newList = Counter(theList)
print newList

which returns:

Counter({'blue': 3, 'red': 2, 'yellow': 1})

How do I access each element and print out something like:

blue - 3
red - 2
yellow - 1
like image 271
Victor Brunell Avatar asked Jan 12 '16 03:01

Victor Brunell


People also ask

How do you access elements in a collection Counter in Python?

Accessing Elements in Python Counter To get the list of elements in the counter we can use the elements() method. It returns an iterator object for the values in the Counter.

What does collections Counter return in Python?

This method returns the list of elements in the counter. Only elements with positive counts are returned.

What does Counter () do in Python?

Counter is a subclass of dict that's specially designed for counting hashable objects in Python. It's a dictionary that stores objects as keys and counts as values. To count with Counter , you typically provide a sequence or iterable of hashable objects as an argument to the class's constructor.

How do you update a Counter in Python?

Python updating counter We can add values to the counter by using update() method.

What are the different containers provided by the collections module?

Some of the built-in containers are Tuple, List, Dictionary, etc. In this article, we will discuss the different containers provided by the collections module. A counter is a sub-class of the dictionary.

Where can I read more about counter in Python collections?

You can read more about Counter in the documentation for the Python collections module. 00:00 Now that you’ve learned the defaultdict, let’s learn some more useful data structures that are all found in the collections module.

What is the use of collections module in Python?

Python Collections Module - GeeksforGeeks. 1 Counters. A counter is a sub-class of the dictionary. It is used to keep the count of the elements in an iterable in the form of an unordered ... 2 OrderedDict. 3 DefaultDict. 4 ChainMap. 5 NamedTuple. More items

How do I retrieve the count values of a counter?

The count values are increased based on the new data, rather than replaced. In this example, the count for a goes from 3 to 4. Once a Counter is populated, its values can be retrieved using the dictionary API. Counter does not raise KeyError for unknown items.


1 Answers

The Counter object is a sub-class of a dictionary.

A Counter is a dict subclass for counting hashable objects. It is an unordered collection where elements are stored as dictionary keys and their counts are stored as dictionary values.

You can access the elements the same way you would another dictionary:

>>> from collections import Counter
>>> theList = ['blue', 'red', 'blue', 'yellow', 'blue', 'red']
>>> newList = Counter(theList)
>>> newList['blue']
3

If you want to print the keys and values you can do this:

>>> for k,v in newList.items():
...     print(k,v)
...
blue 3
yellow 1
red 2
like image 103
Andy Avatar answered Nov 03 '22 15:11

Andy