I am attempting to perform a frequency given a dictionary with a master list containing other dictionaries. Here is my data:
dict = {"costcenters": [{"facid": "A1990", "cc": "postpress", "name": "Factory1"}, {"facid": "A1981", "cc": "prepress", "name": "Factory3"}, {"facid": "A1990", "cc": "prepress", "name": "Factory1"}, {"facid": "A1981", "cc": "prepress", "name": "Factory3"}, {"facid": "A1990", "cc": "postpress", "name": "Factory1"}, {"facid": "A1990", "cc": "digitalpress", "name": "Factory1"}, {"facid": "A7881", "cc": "digitalpress", "name": "Factory2"}]
I want to return the Factory with the most cost centers, so in my abbreviated example here, I would want the for loop to return:
Factory1 - 4
#reason: Factory1 shows up 4 times, Factory3 shows up 2 and Factory2 once)
I don’t need it broken down any further. I have tried the following but am struggling with accessing a list with dictionaries inside and ultimately the ‘name’ key.
elements_count = {} for cc in dict: if cc in elements_count: elements_count[cc] += 1 else: elements_count[cc] = 1 print(max(elements_count))
Nothing prints when I run this, so I don’t have any error messages to share. The simplest solution would be best so that I can understand how to capture frequencies in the future. Thank you! :)
Advertisement
Answer
When I run this I get costcenters
.
I think you want to do:
import sys data = {"costcenters": [{"facid": "A1990", "cc": "postpress", "name": "Factory1"}, {"facid": "A1981", "cc": "prepress", "name": "Factory3"}, {"facid": "A1990", "cc": "prepress", "name": "Factory1"}, {"facid": "A1981", "cc": "prepress", "name": "Factory3"}, {"facid": "A1990", "cc": "postpress", "name": "Factory1"}, {"facid": "A1990", "cc": "digitalpress", "name": "Factory1"}, {"facid": "A7881", "cc": "digitalpress", "name": "Factory2"}]} elements_count = {} for cc in data["costcenters"]: factory = cc["name"] if factory in elements_count: elements_count[factory] += 1 else: elements_count[factory] = 1 max_value=0 min_value=sys.maxsize*sys.maxsize for item in elements_count: if elements_count[item] > max_value: max_value = elements_count[item] factory_max = item elif elements_count[item] < min_value: min_value = elements_count[item] factory_min = item print(f'Maximum Value: {factory_max} - {max_value}') print(f'Minimum Value: {factory_min} - {min_value}')
Output:
Maximum Value: Factory1 - 4
Minimum Value: Factory2 - 1
This assumes your number of factories is no bigger than sys.maxsize
squared (you’re talking many billions here), but obviously this could be changed for the largest realistic number.