{
"id": 1,
"resourceAttributes": {
"siteId": "100"
},
},
{
"id": 2,
"resourceAttributes": {
"siteId": "200"
},
},
{
"id": 3,
"resourceAttributes": {
"siteId": "100"
},
},
I have this kind of json and as a result I want to show output like this
SiteId100 occurance 2
SiteId200 occurance 1
this out put is on basis of occurrences of site id value like if siteId 100 values occurred in 2 objects I need to show number of occurrences with count. I am trying something like getting all possible site ids and then removing duplicates and then finding one by one but seems that is not a neat solution.
CodePudding user response:
you need to use colections.Counter. here's the full code:
from collections import Counter
data = [
{
"id": 1,
"resourceAttributes": {
"siteId": "100"
},
},
{
"id": 2,
"resourceAttributes": {
"siteId": "200"
},
},
{
"id": 3,
"resourceAttributes": {
"siteId": "100"
},
}
]
c = Counter(s.get("resourceAttributes").get("siteId") for s in data)
for k, v in c.items():
print("siteId{} occurance: {}".format(k, v))
output:
siteId100 occurance: 2
siteId200 occurance: 1