python在字典中访问字典中的元素 [英] python accessing elements in a dictionary inside dictionary
本文介绍了python在字典中访问字典中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从防暴服务器得到答复:
I get a response from riot's server:
def main():
api = RiotAPI('dec34559a91-ad8b-4fd2-b49a-bae3b4524522b8a')
summoner_name = str(input("Please enter the summoner ID\n"))
if summoner_name == "":
summoner_name = "zLKida"
r = api.get_summoner_by_name(summoner_name)
print(r)
会打印出这样的内容:
{'zlkidda': {'profileIconId': 539, 'id': 27003987, 'summonerLevel': 30, 'name': 'zLKidda', 'revisionDate': 1444958792000}}
我不知道如何访问字典中的数据. 请注意,它以字典形式返回,而不是字符串或其他任何形式.
I have no idea how I can access the data inside the dictionary. Note that it is returned as a dict not a string or anything else.
我尝试过:
print(r['zlkidda'].['profileIconId'])
推荐答案
删除点:
print(r['zlkidda']['profileIconId'])
或针对您的代码,重新使用summoner_name
变量:
or for your code specifically, reusing teh summoner_name
variable:
print(r[summoner_name]['profileIconId'])
您在此处使用 订阅 ; [...]
从容器中选择一个元素.
You are using subscriptions here; the [...]
selects one element from the container.
.
表示法用于 属性引用 ,将其用于诸如dict
方法之类的东西:
.
notation on the other hand, is used for attribute referencing, use that for things like dict
methods:
print(list(r['zlkidda'].keys()))
这篇关于python在字典中访问字典中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文