Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从字典中获取信息的最佳方法_Python - Fatal编程技术网

Python 从字典中获取信息的最佳方法

Python 从字典中获取信息的最佳方法,python,Python,我正在绞尽脑汁寻找最好的方法,从下面的列表中选出一个名为艺术家的名字 {'status': 'ok', 'results': [{'score': 0.94222, 'id': 'ca222fc1-d1ed-4c30-b21f-eb533cc909aa', 'recordings': [{'artists': [{'id': '84dc4f23-c0b8-4fe1-bbca-a3993ddc8fc2', 'name': 'Primus'}], 'id': '2f6227ba-e061-48b2-

我正在绞尽脑汁寻找最好的方法,从下面的列表中选出一个名为艺术家的名字

{'status': 'ok', 'results': [{'score': 0.94222, 'id': 'ca222fc1-d1ed-4c30-b21f-eb533cc909aa', 'recordings': [{'artists': [{'id': '84dc4f23-c0b8-4fe1-bbca-a3993ddc8fc2', 'name': 'Primus'}], 'id': '2f6227ba-e061-48b2-a700-15e209ed3650', 'title': 'Wynona’s Big Brown Beaver', 'duration': 264}, {'artists': [{'id': '84dc4f23-c0b8-4fe1-bbca-a3993ddc8fc2', 'name': 'Primus'}], 'id': '520ebd07-d12c-429c-a137-19d13303a706', 'title': 'Wynona’s Big Brown Beaver', 'duration': 261}, {'artists': [{'id': '84dc4f23-c0b8-4fe1-bbca-a3993ddc8fc2', 'name': 'Primus'}], 'id': '757439ce-58b1-4808-af3b-3b90092890c1', 'title': 'Wynona’s Big Brown Beaver', 'duration': 262}, {'artists': [{'id': '84dc4f23-c0b8-4fe1-bbca-a3993ddc8fc2', 'name': 'Primus'}], 'id': 'a432eaa4-e5bb-414a-9595-cdaa46d48d6e', 'title': 'Wynona’s Big Brown Beaver', 'duration': 264}, {'artists': [{'id': '84dc4f23-c0b8-4fe1-bbca-a3993ddc8fc2', 'name': 'Primus'}], 'id': 'c344a3f9-1755-4f1b-b227-8a5918713466', 'title': 'Wynona’s Big Brown Beaver', 'duration': 261}, {'artists': [{'id': '84dc4f23-c0b8-4fe1-bbca-a3993ddc8fc2', 'name': 'Primus'}], 'id': 'e658eb35-9d15-45d8-965c-1abcb19c8bc9', 'title': 'Wynona’s Big Brown Beaver', 'duration': 264}]}]}
我正在使用acoustic.match匹配歌曲,上面的列表就是返回的内容

必须有比我现在做的更优雅的东西,那就是:

print(song['results'][0]['recordings'][0]['title'])
有什么建议吗

*****编辑***** 所以,在进一步的回顾中,我试图遵循一些示例代码,但似乎无法使其正常工作

for score, recording_id, title, artist in acoustid.match(apikey, path):

acousticid.match返回上面的字典/列表

如果dict困扰您,您可能需要将其转换为类

class Music():

    def __init__(self, song):
        self.title = song['results'][0]['recordings'][0]['title']
        self.artist = song['results'][0]['recordings'][0]['artist'][0]['name']
        # and so on and so forth


new_song = Music(song)
>>> print new_song.artist
Primus
>>> print new_song.title
Wynona’s Big Brown Beaver

你得到的数据是dict和list的混合。由于列表只能通过索引访问,我认为没有更好的方法来访问它,除非您对数据进行预处理以删除多余的嵌套列表。

字典是一个键值对。要获得值,您需要给它一个键。这是从字典中获取信息的唯一方法。如果这不适合您,请使用不同的数据结构您可以通过使用中间变量使其更具可读性<代码>第一首歌曲歌曲歌曲结果=歌曲['results'][0];录音=第一首歌的结果['recordings'][0];录音['title']还要注意,像这样的字典是用来包装很多你可能想循环的信息的
for song['results']:for recording in song['recordings']:print(recording['title'])
当我将上述内容粘贴到python shell中时,出现语法错误。同样,当我试图在python shell中分配上述内容时。@rajah9:我得到了“Wynona's Big Brown Beaver”。不确定这是好是坏……虽然这个链接可以回答这个问题,但最好在这里包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能无效。-我的帖子里没有链接,你指的是什么?