【问题标题】:Using enumerate for a list of lists (json)使用 enumerate 获取列表列表 (json)
【发布时间】:2020-04-26 18:03:01
【问题描述】:

我正在用 python 看 Spotify 音乐。至今无法在网上找到答案。我想从从 API 检索到的每首歌曲(2000 首歌曲)中获取信息。我有每首歌的艺术家,这是一长串 json,如下所示:

print(artist):

#This is the output:

[[{'track': {'album': {'artists': [{'external_urls': {'spotify': 'https://open.spotify.com/artist/1uiEZYehlNivdK3iQyAbye'},
       'href': 'https://api.spotify.com/v1/artists/1uiEZYehlNivdK3iQyAbye',
       'id': '1uiEZYehlNivdK3iQyAbye',
       'name': 'Tom Misch',
       'type': 'artist',
       'uri': 'spotify:artist:1uiEZYehlNivdK3iQyAbye'},
      {'external_urls': {'spotify': 'https://open.spotify.com/artist/2rspptKP0lPBdlJJAJHqht'},
       'href': 'https://api.spotify.com/v1/artists/2rspptKP0lPBdlJJAJHqht',
       'id': '2rspptKP0lPBdlJJAJHqht',
       'name': 'Yussef Dayes',
       'type': 'artist',
       'uri': 'spotify:artist:2rspptKP0lPBdlJJAJHqht'}]}}}, 

# and so on

由于 artist 变量是列表和字典的列表,当我打印 artist 的长度时,我得到 20:

len(artist)
20

但是,在 artist 变量的每个元素中,有 100 个项目:

len(artist[0])
100

我想遍历 artist 中的所有 2000 个项目,并将它们添加到一个列表中。到目前为止,我的代码非常笨拙:

artist_list = []
i=0
while i<100:

        artist_list.append(artist[0][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[1][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[2][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[3][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[4][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[5][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[6][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[7][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[8][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[9][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[10][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[11][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[12][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[13][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[14][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[15][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[16][i]["track"]["album"]["artists"][0]["name"])
        artist_list.append(artist[17][i]["track"]["album"]["artists"][0]["name"])

        i+=1

缩短此时间的最佳方法是什么?我试过使用 enumerate ,但不知道该怎么做。任何帮助将不胜感激!

【问题讨论】:

  • 请提供至少有 2 首歌曲的艺术家的真实示例,以便我们测试代码。
  • 列表不是JSONJSON 是一种独立于语言的 string 表示一些数据,在你的情况下,它首先需要被解析才能成为一个列表。但是一旦它被解析,你就不再和JSON打交道了,我们也不需要知道它的起源是什么。
  • 在 Spotify API 文档中,它提到每个输出都是 JSON 格式。
  • 是的,然后将其发送给您,然后您使用任何软件来检索它,例如 requestsurllib3 包,然后将其解析为字典列表。使用 Java 的人可能会将其解析为 ArrayListMap 对象(或其他)。我要说的唯一一点是,您向我们展示的不再是 JSON 字符串,而且这个 Python 对象最初来自 JSON 字符串这一事实并不是特别相关。看到json 标签的人可能会认为你有JSON 相关的问题,但你没有。你有一个dict 问题。

标签: python dictionary while-loop spotify enumerate


【解决方案1】:

您可以使用双 for 循环来实现这一点:

artist_list = []
for a in artist:
    for b in a:
        artist_list.append(b["track"]["album"]["artists"][0]["name"])

但是,使用 python 列表推导式更 Pythonic(也更高效):

artist_list = [b["track"]["album"]["artists"][0]["name"] for a in artist for b in a]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-27
    • 2021-10-15
    • 2018-12-23
    • 2021-10-31
    • 1970-01-01
    • 2019-01-15
    • 2019-09-02
    相关资源
    最近更新 更多