python - Dictionary within a list using Counter -


i wanted write function lists counter of dictionary items appear @ least number of times df in other dictionaries.

example:

prune(([{'a': 1, 'b': 10}, {'a': 1}, {'c': 1}], min_df=2) [counter({'a': 1}), counter({'a': 1})] prune(([{'a': 1, 'b': 10}, {'a': 2}, {'c': 1}], min_df=2) [counter({'a': 1}), counter({'a': 2})] 

as can see 'a' occurs twice in 2 dictionaries gets listed in output.

my approach:

from collections import counter def prune(dicto,df=2):    new = counter()    d in dicto:        new += counter(d.keys())    x = {}    key,value in new.items():        if value >= df:            x[key] = value    print counter(x) 

output:

counter({'a': 2}) 

this gives output combined counter. can see, term 'a' appears 2 times on whole , hence satisfies df condition , gets listed in output. now, can correct me desired output.

i suggest:

from collections import counter def prune(dicto, min_df=2):     # create counters     counters = [counter(d.keys()) d in dicto]      # sum counters     total = sum(counters, counter())       # create set keys of high frequency     keys = set(k k, v in total.items() if v >= min_df)      # reconstruct counters using high frequency keys     counters = (counter({k: v k, v in d.items() if k in keys}) d in dicto)      # filter(none, ...) take non empty counters.     return filter(none, counters) 

result:

>>> prune(([{'a': 1, 'b': 10}, {'a': 1}, {'c': 1}], min_df=2) [counter({'a': 1}), counter({'a': 1})] 

Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -