2016-08-02 9 views
0

GMusicAPIを使用してGoogle Playミュージックとやりとりしようとしています(私はMobileClientビューを利用しています)。残念なことに、これはPythonでしかありません。これまでに使ったことのない言語です。私は特定のアーティストを検索する検索を実行しています。私が問題とするのは、一部のアーティスト、私が欲しいアーティストは最初の結果ではないということです。Pythonのネストされた辞書のキーの2番目のインスタンスにアクセス

私の解決策は、トップ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""artistId"の値を取得する必要があります。私はこのような最初のものを呼んでいます。

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

そして、それが正常に動作します。しかし、私はこの

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

のように、そのキーの2番目のインスタンスにアクセスしようと、それは動作しません。どんな助けでも大歓迎です。

+0

ArtistSearch['artist_hits']がリストであるので、2番目の辞書[1]とその中['artist']['name']フィールドをしたい

ArtistSearch['artist_hits'][1]['artist']['name'] # ---^--- 

] '... – kindall

答えて

1

私はあなたが望むことだと思う:1 [ `[0]` `に変更

+0

私はこれを試して誓ったことができ、それはうまくいきませんでした。 – Cistoran

+0

@ Cistoranそれは起こる:) –

関連する問題