【发布时间】:2020-01-22 23:29:53
【问题描述】:
我正在使用 songkick api 来检索音乐会数据,我正在编写一个程序,该程序会为一组艺术家提取即将举行的音乐会信息,这些艺术家已作为数组输入,其中包含艺术家姓名及其伴随的艺术家 ID。 Songkick api。在发出请求时,我正在遍历这个数组,以便我的程序在我作为参数的一部分输入的日期范围内检查每个艺术家的即将上映的节目。到目前为止,我已经能够提取我正在寻找的所有信息,但我注意到在我的输出中有些节目的账单上有多个艺术家,我想知道匹配每个请求迭代的艺术家 ID 的最佳方法使用每个节目的艺术家 ID 并仅输出匹配项。我只对从我的列表中输出艺术家姓名感兴趣,而不是对每个节目收费的每个艺术家感兴趣。例如,如果我正在遍历各种 id 的列表中的一位艺术家是 EarthGang,那么在我的列表中,他们将显示为 ('EarthGang', '8321948'),其中数字是 Songkick 与他们关联的艺术家 id。
如果他们在日期范围内有任何节目,当前的输出将显示一个单独的节目:
[{'artistID': [8321948], 'date': '2020-01-22', 'name': ['EarthGang'], 'city': 'Hollywood, CA, US'}]
输出将显示预订了其他艺术家的音乐会以及:
[{'artistID': [8321948, 5965579], 'date': '2020-01-22', 'name': ['EarthGang', 'Mick Jenkins'], 'city': 'Hollywood, CA, US'}]。
无论每个 formatted_show 的艺术家 ID 数量如何,所需的输出都只为我们正在跟踪的那些输出“艺术家 ID”和“名称”。
转身
[{'artistID': [8321948, 5965579], 'date': '2020-01-22', 'name': ['EarthGang', 'Mick Jenkins'], 'city': 'Hollywood, CA, US'}]
进入
[{'artistID': [8321948], 'date': '2020-01-22', 'name': ['EarthGang'], 'city': 'Hollywood, CA, US'}]
我正在寻找仅显示 EarthGang 名称和 ID 的两个实例。这是songkick json hierarchy的链接
这是一些代码以及我迄今为止尝试过的内容:
import requests
# artists we want to track
artist_ids = [
('ASAP Rocky', '4610868'), ('Petit Biscuit', '8630279'), ('EARTHGANG', '8321948'), ('Taylor Janzen', '9758294')
]
# fetch events based on artist_ids we are tracking and date range
for artist_id in artist_ids:
params = {
'apikey': 'API_KEY',
'min_date': '2020-01-20',
'max_date': '2020-01-28'
}
r = requests.get('https://api.songkick.com/api/3.0/artists/' + artist_id[1] + '/calendar.json', params=params)
response = r.json()
shows = response['resultsPage']['results']
for show in shows:
try:
shows = shows['event']
# reformatting response
formatted_shows = [{
'artistID': [perf['artist']['id'] for perf in s['performance']],
'date': s['start']['date'],
'name': [perf['artist']['displayName'] for perf in s['performance']],
'city': s['location']['city']
}
for s in shows if len(s['performance']) > 0
]
for sub in formatted_shows:
if sub['artistID'] == artist_id[1]:
sub['name'] = artist_id[0]
print(sub)
我附上了一张节目的输出照片,该节目有一位艺术家收费,而另一位艺术家则有多名艺术家收费。假设“Taylor Janzen”是我有兴趣输出的人,而不是其他艺术家,我将如何从 formatted_shows 变量和 Artist_id 变量中匹配她的艺术家 ID,以仅输出她而不是每个艺术家在每次迭代中计费?
【问题讨论】:
-
期望的输出是什么?请编辑帖子以包含该内容
-
@aws_apprentice 刚刚添加。以为我在原始帖子中使它足够透明,但希望这会有所帮助!感谢您浏览!
标签: python json list dictionary match