-4

我有一本字典如下:

Dict = { "key1" : {"subkey1" : "value1" , "subkey2" : "value2" , 

         "key2" : {"subkey1" : "value3" , "subkey2" : "value4 }

我基本上希望能够检查以下值

if (Dict[key][any_subkey] == "value"):

   do something....

我收到 TypeError:列表索引必须是整数,而不是 str ....

任何人都可以帮我解决这个问题。

4

2 回答 2

0

这对我有用:

>>> Dict = { "key1" : {"subkey1" : "value1" , "subkey2" : "value2"} , 
...          "key2" : {"subkey1" : "value3" , "subkey2" : "value4" }}
>>> Dict["key1"]
{'subkey2': 'value2', 'subkey1': 'value1'}
>>> Dict["key1"]["subkey1"]
'value1'
>>> 

你检查过你的语法吗?

于 2013-03-14T02:26:29.807 回答
0

像这样?

>>> Dict = { "key1" : {"subkey1" : "value1" , "subkey2" : "value2" ,
... 
...                    "key2" : {"subkey1" : "value3" , "subkey2" : "value4" }
...                              }
...          }
>>> print Dict["key1"]["key2"]["subkey2"] == "value4"
True
于 2013-03-14T02:26:49.997 回答