user14177720
user14177720

Reputation:

How to get the count for a particular key in the dictionary

My content inside a dictionary is below

I need to now for BusinessArea how many different name key is there, like this need to know Designation also

test=
[ { 'masterid': '1', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Accounting', 'parentname': 'Finance'}, { 'id': '3', 'name': 'Research', 'parentname': 'R & D' } ], 'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }] }, 

{ 'masterid': '2', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Research', 'parentname': '' }, { 'id': '3', 'name': 'Accounting', 'parentname': '' } ], 'Role': [ { 'id': '5032', 'name': 'Tester' }, { 'id': '5033', 'name': 'Developer' } ], 'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }]},

 { 'masterid': '3', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Engineering' }, { 'id': '3', 'name': 'Engineering', 'parentname': '' } ], 'Role': [ { 'id': '5032', 'name': 'Developer' }, { 'id': '5033', 'name': 'Developer', 'parentname': '' } ], 'Designation': [ { 'id': '16', 'name': 'L1' }, { 'id': '20', 'name': 'L2' }, { 'id': '25', 'name': 'L2' }]}]

I want to get the count of masterid of BusinessArea and Designation which is all the names

Expected out is below

[
  {
    "name": "BusinessArea",
    "values": [
      {
        "name": "Accounting",
        "count": "2"
      },
      {
        "name": "Research",
        "count": "2"
      },
      {
        "name": "Engineering",
        "count": "1"
      }
    ]
  },
  {
    "name": "Designation",
    "values": [
      {
        "name": "L1",
        "count": "3"
      },
      {
        "name": "l2",
        "count": "3"
      }
    ]
  }
]

Upvotes: 0

Views: 60

Answers (2)

RoadRunner
RoadRunner

Reputation: 26325

You could count unique names using a nested collections.defaultdict:

from collections import defaultdict
from json import dumps

keys = ["BusinessArea", "Designation"]

group_counts = defaultdict(lambda: defaultdict(int))
for group in test:
    for key in keys:
        names = [item["name"] for item in group[key]]
        unique_names = list(dict.fromkeys(names))
        for name in unique_names:
            group_counts[key][name] += 1

print(dumps(group_counts, indent=2))

Which will give you these counts:

{
  "BusinessArea": {
    "Accounting": 2,
    "Research": 2,
    "Engineering": 1
  },
  "Designation": {
    "L1": 3,
    "L2": 3
  }
}

Then you could modify the result to get the list of dicts you expect:

result = [
    {
        "name": name,
        "values": [{"name": value, "count": count} for value, count in counts.items()],
    }
    for name, counts in group_counts.items()
]

print(dumps(result, indent=2))

Which gives you this:

[
  {
    "name": "BusinessArea",
    "values": [
      {
        "name": "Accounting",
        "count": 2
      },
      {
        "name": "Research",
        "count": 2
      },
      {
        "name": "Engineering",
        "count": 1
      }
    ]
  },
  {
    "name": "Designation",
    "values": [
      {
        "name": "L1",
        "count": 3
      },
      {
        "name": "L2",
        "count": 3
      }
    ]
  }
]

Upvotes: 0

IoaTzimas
IoaTzimas

Reputation: 10624

Try this:

res=[{'name': 'BusinessArea', 'values': []}, {'name': 'Designation', 'values': []}]

listbus=sum([i['BusinessArea'] for i in test], [])
listdes=sum([i['Designation'] for i in test], [])

res[0]['values']=[{'name':i, 'count':0} for i in set(k['name'] for k in listbus)]
res[1]['values']=[{'name':i, 'count':0} for i in set(k['name'] for k in listdes)]

for i in listbus:
    for k in range(len(res[0]['values'])):
        if i['name']==res[0]['values'][k]['name']:
            res[0]['values'][k]['count']+=1

for i in listdes:
    for k in range(len(res[1]['values'])):
        if i['name']==res[1]['values'][k]['name']:
            res[1]['values'][k]['count']+=1

>>> print(res)
[{'name': 'BusinessArea', 'values': [{'name': 'Accounting', 'count': 2}, {'name': 'Research', 'count': 2}, {'name': 'Engineering', 'count': 2}]}, {'name': 'Designation', 'values': [{'name': 'L1', 'count': 3}, {'name': 'L2', 'count': 6}]}]

Upvotes: 1

Related Questions