0

I have a multiple dicts in the list. I want to count the number of occurances of the certain value from the list of dicts.

Here's the list of dict:

a = [{"a":"data1","b":"Nill","c":"data3","d":"Nill"},{"a":"dat1","b":"dat2","c":"dat3","d":"Nill"},{"a":"sa1","b":"sa2","c":"sa3","d":"Nill"}]

In here, i want to count the occurance of Nill in the Key. How to make it possible.

Here's the code i tried:

from collections import Counter
a = [{"a":"data1","b":"Nill","c":"data3","d":"Nill"},{"a":"dat1","b":"dat2","c":"dat3","d":"Nill"},{"a":"sa1","b":"sa2","c":"sa3","d":"Nill"}]
s = 0
for i in a:
    d = (a[s])
    #print(d)
    q = 0
    for z in d:
        print(z)
        z1=d[z]
        #print(z)
        if z1 == "Nill":
            q = q+1
            co = {z:q}
    print(co)

Expected Output:

The count of Nill values in the list of dict

{a:0,b:1,c:0,d:3}
Smack Alpha
  • 1,828
  • 1
  • 17
  • 37

5 Answers5

2

Try this :-


a = [{"a":"data1","b":"Nill","c":"data3","d":"Nill"},{"a":"dat1","b":"dat2","c":"dat3","d":"Nill"},{"a":"sa1","b":"sa2","c":"sa3","d":"Nill"}]

result_dict = {'a' : 0, 'b' : 0,'c' :0, 'd' : 0}
for i in a:
    for key, value in i.items():
        if value =="Nill":
            result_dict[key] +=1

print(result_dict)
2

You can use the Counter directly by counting the boolean expression with something like this that takes advantage of the fact the the counter will count True as 1.

a = [{"a":"data1","b":"Nill","c":"data3","d":"Nill"},{"a":"dat1","b":"dat2","c":"dat3","d":"Nill"},{"a":"sa1","b":"sa2","c":"sa3","d":"Nill"}]

c = Counter()
for d in a:
   c.update({k: v == 'Nill' for k, v in d.items()})

# c => Counter({'a': 0, 'b': 1, 'c': 0, 'd': 3})
Mark
  • 90,562
  • 7
  • 108
  • 148
1

EDIT:

To match the output required:

import pandas as pd
df = pd.DataFrame(a)
occ = {k: list(v.values()).count('Nill') for k,v in df.to_dict().items()}
naivepredictor
  • 898
  • 4
  • 14
  • Certainly not the output OP wanted. – DirtyBit Apr 11 '19 at 06:49
  • 1
    `sum([...], [])` is immensely inefficient; it's multiple orders of magnitude slower than other ways to flatten a list. See https://stackoverflow.com/q/952914/ – iz_ Apr 11 '19 at 06:53
1

Like this?

a = [{"a":"data1","b":"Nill","c":"data3","d":"Nill"},{"a":"dat1","b":"dat2","c":"dat3","d":"Nill"},{"a":"sa1","b":"sa2","c":"sa3","d":"Nill"}]

result = {}
for sub_list in a: # loop through the list
    for key, val in sub_list.items(): # loop through the dictionary
        result[key] = result.get(key, 0) # if key not in dictionary, add it
        if val == 'Nill': # if finding 'Nill', increment that value
            result[key] += 1

for key, val in result.items(): # show result
    print(key, val)
ErikXIII
  • 557
  • 2
  • 12
1

Try this:

from collections import defaultdict

c = defaultdict(int, {i:0 for i in a[0].keys()})
for i in a: 
    for k,v in i.items():
        if v=='Nill': 
            c[k] += 1 

dict(c) will be your desired output.

{'a': 0, 'b': 1, 'c': 0, 'd': 3}
Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59