如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
# #### dict中将key相同的字典合并在一个对象里 """ a = {"a": 1, "b": 2, "c": 1} for k, v in a.iteritems(): tmp.setdefault(v, []).append(k) """ tmp = {} objs = OnlineScoringTeacher.objects. filter ( entry__message_ila__id = 1038 , teacher = 1 , ).select_related( 'entry' ) # 常规方法 for obj in objs: if obj.entry.subject_id not in tmp: tmp.update({ obj.entry.subject_id: [] }) tmp[obj.entry.subject_id].append(obj) # 法二 for obj in objs: tmp.setdefault(obj.entry.subject_id, []).append(obj) |
以上这篇python dict 相同key 合并value的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/YangHeng816/article/details/79142754