简体   繁体   中英

How to count occurrences of specific dict key in list of dicts

I'm trying to count the number of times a specified key occurs in my list of dicts. I've used Counter() and most_common(n) to count up all the keys, but how can I find the count for a specific key? I have this code, which does not work currently:

def Artist_Stats(self, artist_pick):

    entries = TopData(self.filename).data_to_dict()

    for d in entries:
        x = d['artist']
        find_artist = Counter()
        print find_artist[x][artist_pick]

The "entries" data has about 60k entries and looks like this:

[{'album': 'Nikki Nack', 'song': 'Find a New Way', 'datetime': '2014-12-03 09:08:00', 'artist': 'tUnE-yArDs'},]

You could extract it, put it into a list, and calculate the list's length.

key_artists = [k['artist'] for k in entries if k.get('artist')]
len(key_artists)

Edit : using a generator expression might be better if your data is big:

key_artists = (1 for k in entries if k.get('artist'))
sum(key_artists)

2nd Edit :

for a specific artist, you would replace if k.get('artist') with if k.get('artist') == artist_pick

3rd Edit : you could loop as well, if you're not comfortable with comprehensions or generators, or if you feel that enhances code readability

n = 0  # number of artists

for k in entries:
  n += 1 if k.get('artist') == artist_pick else 0

You can add Counter objects together with + . Below is a demonstration:

>>> from collections import Counter
>>> data = [{'a':1, 'b':1}, {'a':1, 'c':1}, {'b':1, 'c':1}, {'a':1, 'c':1}, {'a':1, 'd':1}]
>>> counter = Counter(data[0])
>>> for d in data[1:]:
...     counter += Counter(d)
...
>>> counter
Counter({'a': 4, 'c': 3, 'b': 2, 'd': 1})
>>> counter['a']  # Count of 'a' key
4
>>> counter['d']  # Count of 'd' key
1
>>>

Or, if you want to get fancy, replace the for-loop with sum and a generator expression:

>>> from collections import Counter
>>> data = [{'a':1, 'b':1}, {'a':1, 'c':1}, {'b':1, 'c':1}, {'a':1, 'c':1}, {'a':1, 'd':1}]
>>> counter = sum((Counter(d) for d in data[1:]), Counter(data[0]))
>>> counter
Counter({'a': 4, 'c': 3, 'b': 2, 'd': 1})
>>>

I personally prefer the readability of the for-loop though.

If you mean to count the keys rather than the distinct values to a particular key, then without Counter() :

artist_key_count = 0
for track in entries:
    if 'artist' in track.keys():
        artist_key_count += 1

If you mean to count the number of times each artist appears in your list of tracks, you can also do this without Counter() :

artist_counts = {}
for track in entries:
    artist = track.get('artist')
    try:
        artist_counts[artist] += 1
    except KeyError:
        artist_counts[artist] = 1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM