如何计算在Python的bin中有多少个数据点

我正在学习嵌套字典。到目前为止,我已经学习了如何从嵌套字典中提取特定的数据集。但是现在,我需要计算一个bin中有多少个数据点。

给定的数据如下:

data = {
"1": {'area': 'Administration',  'expenditure': '315'},
"2": {'area': 'Administration', 'expenditure': '120'},
"3": {'area': None, 'expenditure':'314'},
"4":{'area': 'Aids and appliances', 'expenditure': None} ,
"5":{'area': 'Aids and appliances', 'expenditure': '12'},
"6":{'area': 'Administration', 'expenditure': '110'},
"7":{'area': 'Administration', 'expenditure': '300'}
}

代码如下:

from collections import defaultdict

storage = defaultdict(list)

# defaultdict allows to define keys on fly
{storage[d['area']].append(d['expenditure']) for _, d in data.items() if d['area']}

# we have append all expenditure to all areas where the area is not None.
print(storage)
# to go back to normal dictionary with list of expenditures 
results = dict(storage)
print(results)

for k1,v1 in results.items():

    if k1 == "Administration":
        lists = [] 
        for val in v1:
            lists.append(val)
        print(lists)
        for i in range(0, len(lists)):
            lists[i] = int(lists[i])
        print(lists)
        print(max(lists))
        print(min(lists))

如果箱数为6,每个箱的宽度为[max(lists)-min(lists)] // 6 我如何计算落入每个料仓的数据点?非常感谢您的帮助。

评论