Can I use a list comprehension on a list of dictio

2019-07-07 03:01发布

问题:

I want to count the number of times specific values appear in a list of dictionaries. However, I know that some of these dictionaries will not have the key. I do not know which though, because these are results from an API call.

As an example, this code works for key1, because all of the dictionaries have the key.

from collections import Counter
list_of_dicts = [
    {'key1': 'testing', 'key2': 'testing'},
    {'key1': 'prod', 'key2': 'testing'},
    {'key1': 'testing',},
    {'key1': 'prod',},
    {'key1': 'testing', 'key2': 'testing'},
    {'key1': 'testing',},
]

print Counter(r['key1'] for r in list_of_dicts)

I get a nice result of

Counter({'testing': 4, 'prod': 2})

However, if I change that last print to:

print Counter(r['key2'] for r in list_of_dicts)

It fails, because key2 is missing in a couple of the dictionaries.

Traceback (most recent call last):
  File "test.py", line 11, in <module>
    print Counter(r['key2'] for r in list_of_dicts)
  File "h:\Anaconda\lib\collections.py", line 453, in __init__
    self.update(iterable, **kwds)
  File "h:\Anaconda\lib\collections.py", line 534, in update
    for elem in iterable:
  File "test.py", line 11, in <genexpr>
    print Counter(r['key2'] for r in list_of_dicts)
KeyError: 'key2'

How can I use a list comprehension to count the values of key2 and not fail if a dictionary doesn't contain the key?

回答1:

You can explicitly check if key2 is in a dictionary:

Counter(r['key2'] for r in list_of_dicts if 'key2' in r)


回答2:

get lets you specify a default value to return if the key isn't present. thus:

In [186]: Counter(r.get('key2',None) for r in list_of_dicts)
Out[186]: Counter({'testing': 3, None: 3})

Where the None entry tells us how many dictionaries were missing this value. It might be nice to know that. If you don't care, it probably doesn't matter much whether you use this or the if clause.