简体   繁体   English

Python:如何从列表中的字典中提取特定值

[英]Python: How could I extract an specific value from a dictionary within a list

I am receiving the following result after calling reverse_geocoder function and I'd like to retrieve the value of 'name' key into a single variable.在调用 reverse_geocoder function 后,我收到以下结果,我想将“name”键的值检索到单个变量中。 How could I do to get that?.我该怎么做才能得到那个?

[{'lat': '-34.57365',
  'lon': '-58.44924',
  'name': 'Colegiales',
  'admin1': 'Buenos Aires F.D.',
  'admin2': '',
  'cc': 'AR'}]

I am trying with the following code but I m getting an error "TypeError: list indices must be integers or slices, not str":我正在尝试使用以下代码,但出现错误“TypeError:列表索引必须是整数或切片,而不是 str”:

For index, row in df_dummies[mask_nulls].iterrows():
    a = (row['lat'], row['lon'])
    address = rg.search(a)
    suburb = address['name']
    barrios.append(address)

Thanks谢谢

You only have to create a reference variable to the list[0].您只需为列表 [0] 创建一个引用变量。 Then you can use the dictionary['name'] to extract the value.然后您可以使用字典['name'] 来提取值。

As you can notice 'address' is a list and not dictionary.如您所见,“地址”是一个列表,而不是字典。 In case if you are sure that the list always contains only one dictionary, You could use this如果您确定列表始终只包含一个字典,您可以使用这个

address[0]['name']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM