Reputation: 315
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'},]
Upvotes: 5
Views: 20915
Reputation: 28313
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
Upvotes: 13
Reputation: 25550
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
Upvotes: 1
Reputation:
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.
Upvotes: 2