You are viewing a single comment's thread from:

RE: Python Tips 6 - Collections : Container Datatype

in STEMGeeks5 years ago

Didn't know about this before @slashformotion! Interesting, would be useful for complex solutions.

I'm confused with Counter:

>>>counter_instance = Counter(a=4, b=2, c=0, d=-2)
>>>sorted(counter_instance.elements())
['a', 'a', 'a', 'a', 'b', 'b']

Is the value part the occurence or the index? I see the sorted output of the elements() function is only displaying a list of letter with key/index greater than 0.

Sort:  

It's not exactly that.

value is a key and the count is the value in Counter. Counter is a kind of dict, if that help you visualize.

if you want to see how it's done => https://github.com/python/cpython/blob/c77f71f9819022fa3adeb2f710e564a392ff24c6/Lib/collections/init.py#L489