以下代碼不起作用。將項目分配給對象字典
def set_view_counts(self):
"""
Initializes the view counts for all of the Concept objects in the ConceptModel. See Concept for a
description of why this parameter is optional.
"""
for node in self.nodes():
p = PageviewsClient().article_views("en.wikipedia", [node.concept.replace(' ', '_')])
p = [p[key][node.concept.replace(' ', '_')] for key in p.keys()]
p = int(sum([daily_view_count for daily_view_count in p if daily_view_count])/len(p))
node.properties['view_count'] = p
當我檢查我的node.properties
詞典的內容,我覺得4560, 4560, 4560, 4560
。
下面的代碼可以。
def set_view_counts(self):
"""
Initializes the view counts for all of the Concept objects in the ConceptModel. See Concept for a
description of why this parameter is optional.
"""
for node in self.nodes():
p = PageviewsClient().article_views("en.wikipedia", [node.concept.replace(' ', '_')])
p = [p[key][node.concept.replace(' ', '_')] for key in p.keys()]
p = int(sum([daily_view_count for daily_view_count in p if daily_view_count])/len(p))
node.properties = p
當我檢查屬性時,我發現11252, 7367, 3337, 4560
。
這是怎麼回事?
可能只是我或其原因可能很簡單,考慮到你將p存儲在屬性['view_count']而不是屬性? – Torxed
相關將我帶到http://stackoverflow.com/questions/38987/how-can-i-merge-two-python-dictionaries-in-a-single-expression?rq=1,它解決了這個問題。但我仍然想明白這裏的問題是什麼;我可以說這個問題是關於「自我」是一個臨時對象的問題,但我不能爲了我的生活而看到爲什麼。 –
如果這是一個「自我」相關的問題,將需要你所有的代碼,看看你在其他地方如何使用self.nodes – Torxed