Home > Back-end >  best way of counting number of items based on condition python
best way of counting number of items based on condition python

Time:06-10

Hi guys I have multiple conditions to check on a array of objects for example :

My sample array is like this

[{"id":1 , "type":"viw"},{"id":1 , "type":"edit"},{"id":1 , "type":"crt"}]

as these records will be in 10000's I want to calculate total sum of these in best way possible .

Currently doing like this

   view_count = [element for element in user_record if element['type'] == "viw"  ]
   edit_count = [element for element in user_record if element['type'] == "edit"]
   crt_count = [element for element in user_record if element['type'] =="crt"]

   and then len(crt_count) ,len(edit_count) ,len(view_count) 

which seems to me a little expensive . kindly guide can I do it in optimize way ?

and 2nd question can I use OR condition with if in this list comprehension ?

CodePudding user response:

Just collect each of the 'types' into a dictionary key and count them with it's value

result = {}
for element in user_record:
   try:
        result[element["type"]]  = 1
   except KeyError: 
        result[element["type"]] = 1

per comment from @Not A Robot :
"another way for essentially the same idea:"

collections.Counter(map(operator.itemgetter('type'), my_list))

Second Question: Answer is yes you can use or like this:

edit_count = [element for element in user_record if element['type'] == "edit" or element['type'] == 'viw']

CodePudding user response:

If you're concerned with avoiding multiple iterations, just count through one iteration:

sample_arr = [{"id":1 , "type":"viw"},{"id":1 , "type":"edit"},{"id":1 , "type":"crt"}]

view_count, edit_count, crt_count = 0, 0, 0

for d in sample_arr:
    if d['type'] == 'viw':
        view_count  = 1
    elif d['type'] == 'edit':
        edit_count  = 1
    elif d['type'] == 'crt':
        crt_count  = 1

CodePudding user response:

use:

d={}
for dct in sample_arr:
    key = dct['type']
    if d.get(key) is None:
        d[key] = 0
    d[key]  = 1

dict(d)
 {'viw': 1, 'edit': 1, 'crt': 1}

or use defaultdict:

from collections import defaultdict  
e = defaultdict(int)
for dct in sample_arr:
    e[dct['type']]  =1
dict(e)
 {'viw': 1, 'edit': 1, 'crt': 1}

CodePudding user response:

another option without using for loop (but using extra memory):

import pandas as pd

arr = [{"id":1 , "type":"viw"},{"id":1 , "type":"edit"},{"id":1 , "type":"crt"},
       {"id":2 , "type":"viw"},{"id":2 , "type":"edit"},{"id":2 , "type":"crt"}]

df = pd.DataFrame(arr)
res = df['type'].value_counts().to_dict()

>>> res
{'viw': 2, 'edit': 2, 'crt': 2}
  • Related