Python: How to update value of key value pair in nested dictionary?

后端 未结 9 1092
广开言路
广开言路 2021-01-21 20:25

i am trying to make an inversed document index, therefore i need to know from all unique words in a collection in which doc they occur and how often.

i have used this an

9条回答
  •  半阙折子戏
    2021-01-21 21:11

    if (word == keyword and dictionary[keyword][filename] is not None): 
    

    that is not a correct usage i guess, instead try this:

    if (word == keyword and filename in dictionary[keyword]): 
    

    Because, checking the value of a non-existing key raise KeyError. :so You must check if key exists in dictionary...

提交回复
热议问题