假设您有几个字典跟踪每个键的三个浮点值(在子字典中)。您希望能够以添加多个字典中存在的键值的方式合并这些字典。
使用普通的 dict 更新,值被覆盖,所以你子类dict()
:
class StatementDict(dict):
def add(self, statement):
ann_id = statement[0]
lvl_dict = statement[1]
if ann_id in self:
self[ann_id]['skill'] += lvl_dict['skill']
self[ann_id]['knowledge'] += lvl_dict['knowledge']
self[ann_id]['interest'] += lvl_dict['interest']
else:
self[ann_id] = lvl_dict
def update(self, statement_dict):
for statement in statement_dict.iteritems():
self.add(statement)
然后将要合并/添加的字典放入普通字典的键中:
# Small example data that reproduces the error
few_statements = {}
few_statements['linkedin'] = {u'Homerun': {u'skill': 14.0,
u'knowledge': 34.0,
u'interest': 20.0}}
few_statements['tudelft'] = {u'Presentation': {u'skill': 14.0,
u'knowledge': 34.0,
u'interest': 20.0},
u'Future': {u'skill': 16.0,
u'knowledge': 25.33,
u'interest': 2.0},
u'Visual_perception': {u'skill': 20.46,
u'knowledge': 28.35,
u'interest': 4.0}}
few_statements['website'] = {u'Homerun': {u'skill': 1.0,
u'knowledge': 3.0,
u'interest': 2.0}}
few_statements['shareworks'] = {u'Presentation': {u'skill': 8.0,
u'knowledge': 20.0,
u'interest': 12.0},
u'Future': {u'skill': 17.0,
u'knowledge': 26.33,
u'interest': 3.0},
u'Visual_perception': {u'skill': 2.0,
u'knowledge': 3.0,
u'interest': 6.0}}
现在我们应该可以将这些键、值对StatementDict()
一一添加,或者使用该StatementDict.update()
方法。将源字典添加到 StatementDict 的顺序与结果无关。
# First we try updating in one order
small_test1a = StatementDict()
for origin in ("tudelft", "website", "linkedin", "shareworks"):
for st in few_statements[origin].iteritems():
small_test1a.add(st)
# And then in another order
small_test2 = StatementDict()
for origin in ("linkedin", "shareworks", "tudelft", "website"):
for st in few_statements[origin].iteritems():
small_test2.add(st)
print "Different order, same result?", small_test1a == small_test2
# False, but why?
for key in small_test1a:
print "Desired:", key, small_test1a[key]
print "Unexpected:", key, small_test2[key]
唉,添加字典的顺序确实会影响结果。但是为什么,以及发生了什么意外的结果?
Desired: Future {u'skill': 33.0, u'knowledge': 51.66, u'interest': 5.0}
Unexpected: Future {u'skill': 50.0, u'knowledge': 77.99, u'interest': 8.0}
Desired: Presentation {u'skill': 22.0, u'knowledge': 54.0, u'interest': 32.0}
Unexpected: Presentation {u'skill': 30.0, u'knowledge': 74.0, u'interest': 44.0}
Desired: Homerun {u'skill': 15.0, u'knowledge': 37.0, u'interest': 22.0}
Unexpected: Homerun {u'skill': 29.0, u'knowledge': 71.0, u'interest': 42.0}
Desired: Visual_perception {u'skill': 22.46, u'knowledge': 31.35, u'interest': 10.0}
Unexpected: Visual_perception {u'skill': 24.46, u'knowledge': 34.35, u'interest': 16.0}
以第二个顺序添加字典似乎使首先放置的字典的值加倍(添加两次?)。我不明白为什么会这样。如何使所需的添加行为可靠地发生,而与添加顺序无关?
我不明白的另一件事:为什么small_test1a
当我制作一个新的StatementDict()
并用相同的值填充它时,值会发生变化?
运行以下行会导致small_test1a
循环的最终迭代发生变化:
small_test1b = StatementDict()
for origin in ("tudelft", "website", "linkedin", "shareworks"):
small_test1b.update(few_statements[origin])
print "\nDoes .update() function?", small_test1a == small_test1b
print small_test1a
PS使用我的实际数据,根本不会发生任何添加。相反,保留第一个放置的值。这与更新普通字典不同,其中值被覆盖。不幸的是,我无法用少量测试数据重现这种行为。