質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

2回答

322閲覧

python のリスト構造とデータセットにまつわることです。どこから手をつけていいかわかりません

takuyakubo

総合スコア15

データベース

データベースとは、データの集合体を指します。また、そのデータの集合体の共用を可能にするシステムの意味を含めます

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

1グッド

1クリップ

投稿2018/09/12 08:48

前提・実現したいこと

ここに質問の内容を詳しく書いてください。
(例)PHP(CakePHP)で●●なシステムを作っています。
■■な機能を実装中に以下のエラーメッセージが発生しました。

発生している問題・エラーメッセージ

リスト構造とデータセットのことがわからず困っています
AIDEMYさんのブログで乗せられていたコードです

artist_items = spotapi_out['artists']['items'][0]
IndexError: list index out of range

エラーメッセージ artist_items = spotapi_out['artists']['items'][0] IndexError: list index out of range ### 該当のソースコード ```python ソースコード ```import spotipy import json import pandas as pd import matplotlib.pyplot as plt import networkx as nx from community import community_louvain from spotipy.oauth2 import SpotifyClientCredentials client_id = 'xxxxxxxxxxxxxxx' client_secret = 'XXXXXXXXXX' client_credentials_manager = spotipy.oauth2.SpotifyClientCredentials(client_id, cli\ ent_secret) spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager) # アーティスト情報を格納するDataFrameを定義 artist_df = pd.DataFrame(columns=['artist_name','artist_ID','genre','popularity','r\ elated_artist_names']) name = 'Dj Bucher' spotapi_out = spotify.search(q='artist:' + name, type='artist') artist_items = spotapi_out['artists']['items'][0] artist_id = artist_items['id'] artid_list = [artist_id] artname_related_list = [] spotapi_out_related = spotify.artist_related_artists(artist_id) ### 試したこと わからなすぎるので助けてください ### 補足情報(FW/ツールのバージョンなど) Macでライブラリはほぼ最新のものが入っていると思います リスト構造とデータセットがわからないので教えてください
DrqYuto👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

とりあえず、記事通りに spotify の client id と secret 取得して、動かしてみたら
指摘の場所まではエラー出ずに通りましたよ。

IndexError: list index out of range

このようなエラーが出ているということは、サイトからのデータの取得に失敗しているのではないでしょうか。
記事に書いてあるとおりに spotify の client id と secret を取得して設定しましたか?

python

1import spotipy 2import json 3import pandas as pd 4import matplotlib.pyplot as plt 5import networkx as nx 6from spotipy.oauth2 import SpotifyClientCredentials 7 8# Spotify APIの準備 9client_id = '<Your Client ID>' 10client_secret = '<Your Client Secret>' 11 12client_credentials_manager = spotipy.oauth2.SpotifyClientCredentials(client_id, client_secret) 13spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager) 14 15artist_df = pd.DataFrame(columns=['artist_name','artist_ID','genre','popularity','related_artist_names']) 16 17from pprint import pprint 18name = 'Suchmos' 19 20spotapi_out = spotify.search(q='artist:' + name, type='artist') 21pprint(spotapi_out)

txt

1{'artists': {'href': 'https://api.spotify.com/v1/search?query=artist%3ASuchmos&type=artist&offset=0&limit=10', 2 'items': [{'external_urls': {'spotify': 'https://open.spotify.com/artist/0O0hxUrO2PKxZknken3R24'}, 3 'followers': {'href': None, 'total': 138429}, 4 'genres': ['j-dance', 5 'j-rap', 6 'j-rock', 7 'japanese city pop', 8 'japanese r&b'], 9 'href': 'https://api.spotify.com/v1/artists/0O0hxUrO2PKxZknken3R24', 10 'id': '0O0hxUrO2PKxZknken3R24', 11 'images': [{'height': 640, 12 'url': 'https://i.scdn.co/image/fc71ba2ef6b0a5807e195a518541897f4a11ceda', 13 'width': 640}, 14 {'height': 320, 15 'url': 'https://i.scdn.co/image/891ef32e9ff4e49a60e885141cfb3f945c32109d', 16 'width': 320}, 17 {'height': 160, 18 'url': 'https://i.scdn.co/image/7494e1c40d055d3b96ed8b6ccad6604d34a85db1', 19 'width': 160}], 20 'name': 'Suchmos', 21 'popularity': 52, 22 'type': 'artist', 23 'uri': 'spotify:artist:0O0hxUrO2PKxZknken3R24'}, 24 {'external_urls': {'spotify': 'https://open.spotify.com/artist/4otm4Nj3ZNf7QDqjiKp5NK'}, 25 'followers': {'href': None, 'total': 37}, 26 'genres': [], 27 'href': 'https://api.spotify.com/v1/artists/4otm4Nj3ZNf7QDqjiKp5NK', 28 'id': '4otm4Nj3ZNf7QDqjiKp5NK', 29 'images': [], 30 'name': 'Suchmos', 31 'popularity': 13, 32 'type': 'artist', 33 'uri': 'spotify:artist:4otm4Nj3ZNf7QDqjiKp5NK'}], 34 'limit': 10, 35 'next': None, 36 'offset': 0, 37 'previous': None, 38 'total': 2}}

投稿2018/09/12 10:28

tiitoi

総合スコア21956

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

takuyakubo

2018/09/12 10:47

spotify の client id と secretは多分大丈夫だと思います。確認しました。
takuyakubo

2018/09/12 10:50

アーティスト名をサイトと同じにしたらそこの問題は解決しました
takuyakubo

2018/09/12 10:53

その次に File "spotify.py", line 29, in <module> for i in range(relation_size): NameError: name 'relation_size' is not defined と出ましたこれは何が原因と考えられますでしょうか??お時間いただきありがとうございます
guest

0

投稿2018/09/12 08:54

takuyakubo

総合スコア15

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問