数据重组
# 需求说明:将data_source分类统计,并输出为如下data_final的形式:
# data_final ===》
# {
# 'area': [{'place': '南山区', 'amount': 3}, {'place': '宝安区', 'amount': 3}],
# 'type': {'other': 3, 'govenment': 1, 'education': 1, 'business': 1}
# } def class_sum(data_source_list, class_key_name, sum_key_name):
'''对一堆相似字典进行分类统计 :param data_source_list: 原始数据,列表中放字典。如:[{"c":"c1","count":2},{"c":"c2","count":1},{"c":"c1","count":1}]
:param class_key_name: 分类的key名称。如"c"
:param sum_key_name: 统计计数的key名称。如"count"
:return:分类清单 和 对应的统计计数。如:list_class=["c1", "c2"] 和 list_sum=[3, 1]
'''
list_class = []
list_sum = []
for dict_tmp in data_source_list:
sum_tmp = 0
# print(dict_tmp)
if class_key_name in dict_tmp:
if dict_tmp[class_key_name] not in list_class:
list_class.append(dict_tmp[class_key_name])
sum_tmp += dict_tmp[sum_key_name]
list_sum.append(sum_tmp)
else:
sum_index = list_class.index(dict_tmp[class_key_name])
sum_tmp = list_sum[sum_index]+dict_tmp[sum_key_name]
list_sum[sum_index] = sum_tmp
return (list_class, list_sum) data_source = [
{
"town_name": "南山区",
"type": "other",
"count": 1
},
{
"town_name": "南山区",
"type": "govenment",
"count": 1
},
{
"town_name": "南山区",
"type": "education",
"count": 1
},
{
"town_name": "宝安区",
"type": "other",
"count": 2
},
{
"town_name": "宝安区",
"type": "business",
"count": 1
}
] for dict_tmp in data_source:
print(dict_tmp) data_final = {}
data_final['area'] = []
data_final['type'] = {} # 1.1、按照town_name分类和统计count
list_class, list_sum = class_sum(data_source, "town_name", 'count') print(1111111111111)
print(list_class)
print(list_sum) # 2.1 组装area:根据town_name分类和统计
data_final["area"] = list(map(lambda x, y: {"place": x, "amount": y}, list_class, list_sum)) # 1.2、按照type分类和统计count
list_class, list_sum = class_sum(data_source, "type", 'count') print(2222222222222)
print(list_class)
print(list_sum) # 2.2 组装type:根据type分类和统计
for c in list_class:
data_final["type"][c] = list_sum[list_class.index(c)]
print(data_final) # 想要的 {'area': [{'amount': 3, 'place': '南山区'}, {'amount': 3, 'place': '宝安区'}], 'type':{'other': 3, 'govenment': 1, 'education': 1, 'business': 1}}
输出: