Collections counter list comprehension

I want to write some tests to analyse the efficiency of different operations in python, namely a comparison of dictionary comprehensions and dict generators.

To test this out, I thought I would try a simple example: count the number of words in a list using dictionaries.

Now I know that you can do this using collections.Counter [as per an answer here: How can I count the occurrences of a list item in Python?], but my objective was to test performance an memory.

One "long-hand" way is to do it in a basic loop.

The result:

{'a': 62, 'aback': 1, 'able': 1, 'abolished': 2, 'about': 6, 'accept': 1, 'accepted': 1, 'accord': 1, 'according': 1, 'across': 1, ...

Then I got a bit stuck trying to do the same in a dictionary comprehension:

dict2 = { w: 1 if not dict2.get[w] else dict2.get[w] + 1 for w in words }

I got an error:

NameError: global name 'dict2' is not defined

I tried defining the dict up front:

dict2 = {} dict2 = { w: 1 if not dict2.get[w] else dict2.get[w] + 1 for w in words } pprint[dict2]

But of course the counts are all set to 1:

{'a': 1, 'aback': 1, 'able': 1, 'abolished': 1, 'about': 1, 'accept': 1, 'accepted': 1, 'accord': 1, 'according': 1, 'across': 1, ...

I had a similar problem with dict comprehension:

dict3 = dict[ [w, 1 if not dict2.get[w] else dict2.get[w] + 1] for w in words]

So my question is: how can I use a dictionary comprehension/generator most efficiently to count the number of occurrences in a list?

Update: @Rawing suggested an alternative approach {word:words.count[word] for word in set[words]} but that would circumvent the mechanism I am trying to test.

Video liên quan

Chủ Đề