在Python中访问嵌套字典中键的第二个实例

2024-04-29 02:12:42 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我利用GMusicAPI来尝试与googleplaymusic交互(我使用MobileClient视图)。不幸的是,它只在Python中,一种我从未使用过的语言。我正在运行一个搜索,它将搜索给定的艺术家。我遇到的问题是,有些艺术家,我想要的艺术家并不是第一个结果。在

我的解决方案是只搜索(此搜索返回一个字典)前5-10位艺术家,然后循环搜索,直到字典键中的名称与搜索查询匹配。在

这是我从搜索中得到的词典的一部分。这就是"artist_hits"部分。在

[{'artist': {'name': 'Tiësto', 'artistArtRef': 'http://lh3.googleusercontent.com/wfrs3FuLMoZ7MMfESLOE7kXw9pR9usqZsR-OCo7GW544aqHfj_WMo_YYeETdAmUGQU9fJW7D', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': False, 'url': 'http://lh3.googleusercontent.com/wfrs3FuLMoZ7MMfESLOE7kXw9pR9usqZsR-OCo7GW544aqHfj_WMo_YYeETdAmUGQU9fJW7D', 'aspectRatio': '2'}], 'kind': 'sj#artist', 'artistId': 'Agzf4r7d54azste62qh6z2q7bcy', 'artist_bio_attribution': {'license_url': 'http://creativecommons.org/licenses/by-sa/4.0/legalcode', 'kind': 'sj#attribution', 'source_title': 'Wikipedia', 'license_title': 'Creative Commons Attribution CC-BY-SA 4.0', 'source_url': 'http://en.wikipedia.org/wiki/Ti%C3%ABsto'}}, 'type': '2'}, 

{'artist': {'name': 'Skrillex', 'artistArtRef': 'http://lh3.googleusercontent.com/wUw1KU9e6VOO7tSNFjKvCljL05Fud1w5mvt8UTbs5G7fBS4sFGphEOIs0EUXBwF9CHuGFEufgQ', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': False, 'url': 'http://lh3.googleusercontent.com/wUw1KU9e6VOO7tSNFjKvCljL05Fud1w5mvt8UTbs5G7fBS4sFGphEOIs0EUXBwF9CHuGFEufgQ', 'aspectRatio': '2'}], 'kind': 'sj#artist', 'artistId': 'Aqy2vtuiohb4rdrakrtbphxbdme', 'artist_bio_attribution': {'kind': 'sj#attribution', 'source_title': 'artist representative'}}, 'type': '2'}, 

{'artist': {'kind': 'sj#artist', 'artistId': 'Achiqpfv5ncaoobeiu6vfmf2jf4', 'name': 'Marshmello', 'artistArtRef': 'http://lh3.googleusercontent.com/KAqga3a8rX1Tam5FSyWGUcT56Zm9uDoyei2vty1Xra8CApKn1vi5Nb9-nNt70U4Q6rtuOA9KXQ', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh3.googleusercontent.com/KAqga3a8rX1Tam5FSyWGUcT56Zm9uDoyei2vty1Xra8CApKn1vi5Nb9-nNt70U4Q6rtuOA9KXQ', 'aspectRatio': '2'}, {'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh3.googleusercontent.com/Zp3xcR0sc0LDnFuYy7sY-l8ggvEmPjFK_UruOSaZk25jKIYZKDsZA382WIyXqptClH91oI7QzQ', 'aspectRatio': '1'}]}, 'type': '2'}, 

{'artist': {'name': 'deadmau5', 'artistArtRef': 'http://lh3.googleusercontent.com/rNh6zCCuiJCgEWldulkYIbpfP33W9K0ULAPsWrDThPdz_naqHR_bznxRFgoOwMRr5V0ur-O-', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': False, 'url': 'http://lh3.googleusercontent.com/rNh6zCCuiJCgEWldulkYIbpfP33W9K0ULAPsWrDThPdz_naqHR_bznxRFgoOwMRr5V0ur-O-', 'aspectRatio': '2'}], 'kind': 'sj#artist', 'artistId': 'Atngmlxlixottlthpcypidmxlu4', 'artist_bio_attribution': {'license_url': 'http://creativecommons.org/licenses/by-sa/4.0/legalcode', 'kind': 'sj#attribution', 'source_title': 'Wikipedia', 'license_title': 'Creative Commons Attribution CC-BY-SA 4.0', 'source_url': 'http://en.wikipedia.org/wiki/Deadmau5'}}, 'type': '2'}, 

{'artist': {'kind': 'sj#artist', 'artistId': 'A3akm3hkaas6r4mp3nmhn2d7vom', 'name': 'Marshmello', 'artistArtRef': 'http://lh3.googleusercontent.com/sU63V5nC1xAYuYLfd-WCzmfP5Z1iYslm8xntUe6HPVRmvIEs9QCwaxRgcaQy1Eh8HbSEwEMunQ', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh3.googleusercontent.com/sU63V5nC1xAYuYLfd-WCzmfP5Z1iYslm8xntUe6HPVRmvIEs9QCwaxRgcaQy1Eh8HbSEwEMunQ', 'aspectRatio': '2'}, {'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh4.ggpht.com/6cv48SUFmOIXMjXmgs6KCw6NViHEQ2w2YRN2hhQ6HBAcVggh9-L882bYwH5VKZ2YqloodT3rNg', 'aspectRatio': '1'}]}, 'type': '2'}, 

{'artist': {'kind': 'sj#artist', 'artistId': 'Aatki3zj7b6fnemq2qldpjmfg74', 'name': 'Marshmellow Overcoat', 'artistArtRef': 'http://lh3.googleusercontent.com/-0spMaLVdzkwkoLgfNPAAL-KMUX1ulOYEMN7sAs_i2kZtFhq-CmPCKHECF_wRtKeTUnFWKViPg', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh3.googleusercontent.com/-0spMaLVdzkwkoLgfNPAAL-KMUX1ulOYEMN7sAs_i2kZtFhq-CmPCKHECF_wRtKeTUnFWKViPg', 'aspectRatio': '2'}, {'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh3.ggpht.com/BfiwEzdKnrAonw4myuyPu6p7GlsAhpd3ZA4E4wQQ-rkygRikCYKRwOWTTM1y3C2oYhPkqXsq0Q', 'aspectRatio': '1'}]}, 'type': '2'}, 

{'artist': {'kind': 'sj#artist', 'artistId': 'Aik7qbnj6ml3i7fpya4rwxjswqq', 'name': 'Marshmellow', 'artistArtRef': 'http://lh3.googleusercontent.com/9UXduVP_4LP702bqvQKU8NKBh9sH_1P9LLYXYQ_IcRcVQUNXqMiSY1dykYdWlGR21NsakeOK', 'artistArtRefs': [{'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh3.googleusercontent.com/9UXduVP_4LP702bqvQKU8NKBh9sH_1P9LLYXYQ_IcRcVQUNXqMiSY1dykYdWlGR21NsakeOK', 'aspectRatio': '2'}, {'kind': 'sj#imageRef', 'autogen': True, 'url': 'http://lh3.googleusercontent.com/xmuuJ_XtgD1CBq7ZBKYZdo0gp7s9oiXZeNQ1lEhDfsXIWhAv2JD3w7IyM54y5OFpw8cAOEaV2Q', 'aspectRatio': '1'}]}, 'type': '2'}]

我的问题是如何访问超过第一个"artist"键的内容?我需要从中得到"name"和{}的值。我给第一个打电话。在

{cd5}

而且效果很好。但是,我试图像这样访问这个键的第二个实例

ArtistSearch['artist_hits'][0]['artist'][1]['name']

但它不起作用。任何帮助都将不胜感激。在


Tags: namecomtruehttpurlartistkindautogen