简体   繁体   中英

Count frequency using generator in python

I built a generator that yields around 6 million dictionaries, and I want to count the frequency of a value in the dictionary.

For instance, each dictionary looks like below, and I want to count the value of the key 'state'

dict1 = {'name':'Jane','state':'Alabama'}
dict2 = {'name':'Joe','state':'California'}

and I want the result

{'Alabama':1,'California:1}

I know that I can append the 'state' value in a list and the use from collections import Counter , but I don't want to save the values in a list because the generator yields around 6 million dictionaries.

Is there a way to count the frequency in this case? Or any other memory efficient way would be helpful.

For now, I think maybe one way is to return a dictionary where key is state and value is the count using a for loop?

That should be straightforward. Assuming every yielded value contains the state key:

result = Counter(d["state"] for d in my_generator())

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