2

我的字典是这样的:

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}ie

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

我尝试过的任何变化都会给我带来意想不到的输出。谁能给我正确的语法?

4

5 回答 5

3

这个 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}]
            }

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}

或者,只有一个字典列表,列表中的位置对应于文档索引:

>>> 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
于 2012-08-03T14:22:05.087 回答
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()) ]
于 2012-08-03T14:14:50.173 回答
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}
于 2012-08-03T14:17:02.073 回答
1

以及更多的在线解决)

sum(reduce(lambda x, y: x+y, [d.values() for d in v for _,v in docScores.iteritems()]))
于 2012-08-03T14:20:36.957 回答
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())  ) ) 
于 2012-08-03T14:26:58.960 回答