从Python中的dict按键检索值

时间:2018-02-20 10:21:15

标签: python python-3.x

Guys有任何方式可以使用mylist['status']

检索状态值
mylist = {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Winnetka",
               "short_name" : "Winnetka",
               "types" : [ "locality", "political" ]
            },
         ],

   ],
   "status" : "OK"
}

2 个答案:

答案 0 :(得分:3)

更正您的代码如下(缺少右括号}

mylist = {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Winnetka",
               "short_name" : "Winnetka",
               "types" : [ "locality", "political" ]
            },
         ],
      }
   ],
   "status" : "OK"
}

和之后你可以致电mylist['status']

> mylist['status'] 
OK

答案 1 :(得分:1)

我已经重新格式化了下面的代码,因此对您来说更有意义:

mylist = {"results" : [{"address_components" : [{"long_name" : "Winnetka",
                                                 "short_name" : "Winnetka",
                                                 "types" : ["locality", "political"]}]}],
          "status" : "OK"}

mylist['status']  # 'OK'