从嵌套字典中找出某些值 [英] Working out certain values from a nested dictionary

查看:116
本文介绍了从嵌套字典中找出某些值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个嵌套字典:

  myDict = {'a':{1:2,
2:163,
3:12,
4:67,
5:84
},
'about':{1:27,
2:45,
3:21,
4:10,
5:15
},
'an':{1:3,
2:15,
3:1,
4:312,
5:100
}
'expectedate':{1:1,
2 :5,
3:0,
4:8,
5:7
}
'apple':{1:0,
2: 5,
3:0,
4:10,
5:0
}
}

外键是一个单词,内键是该单词所含的文件,值是该单词在该文件中出现的次数。



我想要解决两件事情:



第一个是每个单词总共出现的次数,因此a将是328。



第二个是包含每个单词的文件数,因此a为5,apple为2。



我猜这些值将是两个字典,而不是嵌套的,而不是嵌套,即{word:total count}和{word:num的文件出现在}中。



编辑:我想要解决的另一件事是每个文件的字矢量大小。



所以对于文件1,它将是sqrt 2 ^ 2 + 27 ^ 2 + 3 ^ 2 + 1 ^ 2 + 0 ^ 2)

解决方案

IIUC中,您可以直接用字典理解。



给定单词字典中所有值的总和: / p>

 >>> {k:sum(d.values())for k,d in myDict.items()} 
{'a':328,'about':118,'apple':15,'expectate':21 ,'a':431}

子字典中大于零的值数: p>

 >>> {k:sum(v> 0 for v in d.values())for k,d in myDict.items()} 
{'a':5,'about':5,'apple' 2,'expectate':4,'an':5}

最后一个依赖于事实那个 int(True)== 1 int(False)== 0 ,所以我们不需要写入 1如果v> 0 else 0 或某事,但可以将布尔值相加。


Say I had a nested dictionary:

      myDict = { 'a': { 1: 2,
                        2: 163,
                        3: 12,
                        4: 67,
                        5: 84
                        },
             'about': { 1: 27,
                        2: 45,
                        3: 21,
                        4: 10,
                        5: 15
                        },
                'an': { 1:  3,
                        2: 15,
                        3:  1,
                        4:312,
                        5:100
                        }
        'anticipate': { 1:  1,
                        2:  5,
                        3:  0,
                        4:  8,
                        5:  7
                        }
             'apple': { 1:  0,
                        2:  5,
                        3:  0,
                        4:  10,
                        5:  0
                        }
           }

The outer key is a word, the inner keys are the files that that word contains and the values are the number of times said word appears in that file.

I want to work out 2 things:

The first is the number of times each word appears in total so for 'a' it would be 328.

The second is the number of files containing each word so it would be 5 for 'a' but 2 for 'apple'.

I'm guessing these 'values' will be two dictionaries but standard ones rather than nested i.e. {word : total count} and {word : num of files it appears in}.

Edit: Another thing I'd like to work out is each file's word vector magnitude.

So for file 1 it would be sqrt (2^2 + 27^2 + 3^2 + 1^2 + 0^2)

解决方案

IIUC, you can do this straightforwardly with dictionary comprehensions.

Total of all the values in a given word's dictionary:

>>> {k: sum(d.values()) for k,d in myDict.items()}
{'a': 328, 'about': 118, 'apple': 15, 'anticipate': 21, 'an': 431}

Number of values in the subdictionary which are greater than zero:

>>> {k: sum(v > 0 for v in d.values()) for k,d in myDict.items()}
{'a': 5, 'about': 5, 'apple': 2, 'anticipate': 4, 'an': 5}

This last one relies upon the fact that int(True) == 1 and int(False) == 0, so we don't need to write 1 if v > 0 else 0 or something, but can sum the booleans instead.

这篇关于从嵌套字典中找出某些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆