基于python中的其他字典值创建多个字典
我有一个包含许多词典的列表。每个字典都表示在我的应用程序中发生的更改。“更改”词典包含以下条目:基于python中的其他字典值创建多个字典,python,dictionary,Python,Dictionary,我有一个包含许多词典的列表。每个字典都表示在我的应用程序中发生的更改。“更改”词典包含以下条目: userid: The user ID for a user ctype: A reference to a change type in my application score: A score ctype可以是12个不同字符串中的一个,包括“删除”、“新建”、“编辑”和其他字符串。以下是其中一个“更改”词典的示例: {'userid':2, 'score':10, 'ctype':'edit
userid: The user ID for a user
ctype: A reference to a change type in my application
score: A score
ctype可以是12个不同字符串中的一个,包括“删除”、“新建”、“编辑”和其他字符串。以下是其中一个“更改”词典的示例:
{'userid':2, 'score':10, 'ctype':'edit'}
from collections import defaultdict
dict1 = {'userid': 1, 'score': 10, 'ctype': 'edit'}
dict2 = {'userid': 2, 'score': 13, 'ctype': 'other'}
dict3 = {'userid': 1, 'score': 1, 'ctype': 'edit'}
list_of_dicts = [dict1, dict2, dict3]
user_dict = defaultdict(lambda: defaultdict(int))
for d in list_of_dicts:
userid = d['userid']
user_dict[userid]['score'] += d['score']
user_dict[userid][d['ctype']] += 1
# user_dict is now
# defaultdict(<function <lambda> at 0x02A7DF30>,
# {1: defaultdict(<type 'int'>, {'edit': 2, 'score': 11}),
# 2: defaultdict(<type 'int'>, {'score': 13, 'other': 1})})
我的问题是,如何创建一个字典,在这个大字典列表中为每个用户聚合所有更改类型?我想将每个变更字典的分数相加,以创建总分,并将每个ctype实例相加,以获得每个实例的计数。目标是创建一个字典列表,每个字典如下所示:
{'userid':2, 'score':325, 'deletion':2, 'new':4, 'edit':9}
我一直在努力解决这个问题,但我对python还很陌生,不知道如何计算实际的更改类型。另一个让我感兴趣的部分是如何根据“userid”引用字典。如果有人能给出一个答案,我相信这一切对我来说都会变得非常明显。非常感谢您的帮助。您能帮我吗
(模型不是真正的python。)
您可以将dict作为值嵌套在python字典中。要根据
userid
索引字典,您可以使用字典字典字典:
{'userid':2, 'score':10, 'ctype':'edit'}
from collections import defaultdict
dict1 = {'userid': 1, 'score': 10, 'ctype': 'edit'}
dict2 = {'userid': 2, 'score': 13, 'ctype': 'other'}
dict3 = {'userid': 1, 'score': 1, 'ctype': 'edit'}
list_of_dicts = [dict1, dict2, dict3]
user_dict = defaultdict(lambda: defaultdict(int))
for d in list_of_dicts:
userid = d['userid']
user_dict[userid]['score'] += d['score']
user_dict[userid][d['ctype']] += 1
# user_dict is now
# defaultdict(<function <lambda> at 0x02A7DF30>,
# {1: defaultdict(<type 'int'>, {'edit': 2, 'score': 11}),
# 2: defaultdict(<type 'int'>, {'score': 13, 'other': 1})})
从集合导入defaultdict
dict1={'userid':1,'score':10,'ctype':'edit'}
dict2={'userid':2,'score':13,'ctype':'other'}
dict3={'userid':1,'score':1,'ctype':'edit'}
目录列表=[目录1,目录2,目录3]
user_dict=defaultdict(lambda:defaultdict(int))
对于目录列表中的d:
userid=d['userid']
用户dict[userid]['score']+=d['score']
用户dict[userid][d['ctype']]+=1
#用户_dict现在是
#defaultdict(,
#{1:defaultdict(,{'edit':2,'score':11}),
#2:defaultdict(,{'score':13,'other':1})
在这个例子中,我使用了一个defaultdict
,以避免在每次迭代时检查键d['ctype']
是否存在
change_types = ['deletion', 'new', 'edit', ...]
user_changes = {}
for change in change_list:
userid = change['userid']
if not userid in user_changes:
aggregate = {}
aggregate['score'] = 0
for c in change_types:
aggregate[c] = 0
aggregate['userid'] = userid
user_changes[userid] = aggregate
else:
aggregate = user_changes[userid]
change_type = change['ctype']
aggregate[change_type] = aggregate[change_type] + 1
aggregate['score'] = aggregate['score'] + change['score']
实际上,为聚合创建一个类是一个好主意。这里删除数据的关键是拥有一个字典,其中每个键都是userid,每个条目都是与该userid相关的数据
final_data = {}
for entry in data:
userid = entry["userid"]
if userid not in final_data:
final_data[userid] = {"userid": userid, "score": 0}
final_data[userid]["score"] += entry["score"]
if not entry["ctype"] in final_data[userid]:
final_data[userid][entry["ctype"]] = 1
else:
final_data[userid][entry["ctype"]] += 1
如果您想将结果作为字典列表,只需使用
final\u data.values()
在user\u dict中选择userid而不是has\u key
。您也可以选择user\u dict=defaultdict(lambda:defaultdict(int))