我的字典看起来像这样:
docScores = {0:[{u'word':2.3},{u'the':8.7},{u'if':4.1},{u'Car':1.7}],
1:[{u'friend':1.2},{u'a':5.2},{u'you':3.8},{u'person':0.8}],
...
29:[{u'yard':1.5},{u'gardening':2.8},{u'paint':3.7},{u'brush':1.6}]
}
我想对每个列表的每个内部字典的值求和并将其存储在新的字典中,新的字典值为{0:2.3+8.7+4.1+1.7, 1:1.2+5.2+3.8+0.8, ... etc}
,即
for x in docScores[0]: #{0:
for x in docScores[0][0].values(): #{,2.3}.
sum = sum+x #where sum = 0 before loop
docSum[0] = sum
repeat this loop for every document
我尝试的任何变化都会给我意想不到的输出。任何人都可以给我正确的语法吗?
答案 0 :(得分:3)
这个词汇理解有效:
docScores = {0:[{u'word':2.3},{u'the':8.7},{u'if':4.1},{u'Car':1.7}],
1:[{u'friend':1.2},{u'a':5.2},{u'you':3.8},{u'person':0.8}],
29:[{u'yard':1.5},{u'gardening':2.8},{u'paint':3.7},{u'brush':1.6}]
}
sum_d={k:sum(d.values()[0] for d in v) for k,v in docScores.items()}
print sum_d
打印:
{0: 16.8, 1: 11.0, 29: 9.6}
但是,更改数据结构可能更容易。你可能会有一个词典:
>>> NdocScores = {0:{u'word':2.3,u'the':8.7,u'if':4.1,u'Car':1.7},
... 1:{u'friend':1.2,u'a':5.2,u'you':3.8,u'person':0.8},
... 29:{u'yard':1.5,u'gardening':2.8,u'paint':3.7,u'brush':1.6}
... }
允许直接访问每个文档数据:
>>> NdocScores[0]
{u'Car': 1.7, u'the': 8.7, u'word': 2.3, u'if': 4.1}
>>> NdocScores[0][u'Car']
1.7
>>> sum(NdocScores[1].values())
11.0
>>> NdocScores[29]
{u'gardening': 2.8, u'yard': 1.5, u'brush': 1.6, u'paint': 3.7}
或者,只需要一个列表,其中列表中的位置对应于doc索引:
>>> lofdicts=[v for k,v in NdocScores.items()]
>>> lofdicts
[{u'Car': 1.7, u'the': 8.7, u'word': 2.3, u'if': 4.1}, {u'a': 5.2, u'person': 0.8, u'you': 3.8, u'friend': 1.2}, {u'gardening': 2.8, u'yard': 1.5, u'brush': 1.6, u'paint': 3.7}]
>>> lofdicts[0]
{u'Car': 1.7, u'the': 8.7, u'word': 2.3, u'if': 4.1}
>>> sum(lofdicts[1].values())
11.0
答案 1 :(得分:2)
new_dict={}
docScores = {0:[{u'word':2.3},{u'the':8.7},{u'if':4.1},{u'Car':1.7}],
1:[{u'friend':1.2},{u'a':5.2},{u'you':3.8},{u'person':0.8}],
29:[{u'yard':1.5},{u'gardening':2.8},{u'paint':3.7},{u'brush':1.6}]
}
for k,v in docScores.items():
new_dict[k]=sum( sum(d.values()) for d in v )
print (new_dict) #{0: 16.8, 1: 11.0, 29: 9.6}
正如其他人所提到的,你可以将其变成字典理解(python 2.7 +):
new_dict = {k : sum( sum(d.values()) for d in v ) for k,v in docScores.items() }
但是在这一点上,我认为理解变得非常难以理解(因此我不会这样做)。
此外,有人应该指出,如果你的所有字典键都是从0开始到29的连续整数,你可能不应该使用字典存储这些数据 - 也许列表更合适。 ..
修改强>
使用列表:
new_list = [sum( sum(d.values()) for d in v ) for _,v in sorted(docScores.items()) ]
答案 2 :(得分:1)
>>> doc_scores = {
0: [{u'word': 2.3}, {u'the': 8.7}, {u'if': 4.1}, {u'Car': 1.7}],
1: [{u'friend': 1.2}, {u'a': 5.2}, {u'you': 3.8}, {u'person': 0.8}],
29: [{u'yard': 1.5}, {u'gardening': 2.8}, {u'paint': 3.7}, {u'brush': 1.6}]
}
>>> dict((k, sum(n for d in v for n in d.itervalues()))
for k, v in doc_scores.iteritems())
{0: 16.8, 1: 11.0, 29: 9.6}
如果列表中的每个词组中只有一个值,您可以缩短它:
>>> dict((k, sum(d.values()[0] for d in v)) for k, v in doc_scores.iteritems())
{0: 16.8, 1: 11.0, 29: 9.6}
答案 3 :(得分:1)
更多oneline解决方案)
sum(reduce(lambda x, y: x+y, [d.values() for d in v for _,v in docScores.iteritems()]))
答案 4 :(得分:0)
docScores = {0:[{u'word':2.3},{u'the':8.7},{u'if':4.1},{u'Car':1.7}],
1:[{u'friend':1.2},{u'a':5.2},{u'you':3.8},{u'person':0.8}],
2:[{u'yard':1.5},{u'gardening':2.8},{u'paint':3.7},{u'brush':1.6}]
}
result = dict(enumerate(sum (sum(word.values()) for word in word_list[1]) for word_list in sorted(docScores.items()) ) )