下面的代碼:求和鍵在字典中
for j in reversed(range(0,15)):
print i
successors = g.successors(totuple(total_nodes[j,:]))
array = [0,0,0,0]
a=0
i = i+1
for succ in successors:
print g.node[succ]
array[a]=g.node[succ]
a+=1
print array
print sum(item['key'] for item in array)
產生以下輸出:
1
{'key': 0.0}
{'key': 0.39730768970355257}
{}
{'key': 0.0}
[{'key': 0.0}, {'key': 0.39730768970355257}, {}, {'key': 0.0}]
- 我不明白爲什麼有些節點沒有鍵{}。但是,我想忽略這些。
- 我要總結這些值或 '鑰匙'
因此,基於我嘗試過其他問題:
print sum(item['key'] for item in array)
生產:KeyError: 'key'
print sum([i for i in array.values()])
生產:AttributeError: 'list' object has no attribute 'values'