我有一个从谷歌学者那里搜集来的词典列表,看起来像这样,
WRT_Citations = {Citations: 201, year: 2008, Title: Something, Author: Authors, Url: Url} {Citations: 108, year: 2006, Title: Something, Author: Authors, Url: Url}{Citations: 100, year: 2009, Title: Something, Author: Authors, Url: Url}我把它放在这个for循环中,使它更有序。
for Citations in Wrt_Citations:
print "Print Citations ", Citations这给出了一个更有序的列表,
output = {Citations: 201, year: 2008, Title: Something, Author: Authors, Url: Url}
{Citations: 108, year: 2006, Title: Something, Author: Authors, Url: Url}
{Citations: 100, year: 2009, Title: Something, Author: Authors, Url: Url}我想要得到总引文量,即201 + 108 + 100 = 409。我已经能够通过做以下事情来获得单独的引用,
Cites = dict.values(Citations)[0]
print cites = 201
108
100所以我尝试使用sum(dict.values( citations ))来获得总的引文量,但是这仅仅给出了TypeError:'int‘对象是不可迭代的。
任何帮助都会很高兴例外,我一直在自学,通过tril和error python在过去的几周里,所以一些术语可能不正确,提前道歉,哦,列表已经排序了两次,重复项也被删除了,你知道的。
发布于 2013-07-16 18:49:09
使用生成器表达式循环遍历所有引用字典:
sum(d['Citations'] for d in WRT_Citations)请注意,使用dict.values(Citations)[0]是Citations.values()[0]的一种非常拐弯抹角的说法,这是一种不正确和不可靠的Citations['Citations']说法(在名为Citations的字典中访问与'Citations'键相关联的值)。
发布于 2013-07-16 18:51:29
您的代码结构和语法是非常错误的,但假设它们是正确的(如下所示),您将如何执行您想要的操作:
WRT_Citations = [
{'Citations': 201, 'year': 2008, 'Title': 'Something', 'Author': 'Authors', 'Url': 'Url'},
{'Citations': 108, 'year': 2006, 'Title': 'Something', 'Author': 'Authors', 'Url': 'Url'},
{'Citations': 100, 'year': 2009, 'Title': 'Something', 'Author': 'Authors', 'Url': 'Url'}]
total_citations = sum(d['Citations'] for d in WRT_Citations)
# 409https://stackoverflow.com/questions/17674514
复制相似问题