我已经开始了一个小项目,以便在python上取得更好的成绩。对于这个项目,我需要从标题中包含单词rp的人那里获取user_ids。
我知道如何使用以下代码从json api响应中获取所有user_id#s:
[x['user_id'] for x in data['data']]
但现在我想只获取其标题中包含单词rp的用户的user_id。但我似乎无法弄清楚如何做到这一点。
来自twitch API的json响应:{
'data':[
{
'id':'27144974352',
'user_id':'23308993',
'game_id':'32982',
'community_ids':[
'af543777-83ec-402d-8245-30da1409e22a',
'c0294643-085c-4943-94ab-f0e1ba090e23',
'd74020e4-53af-48c3-b3d1-f23500a490c8'
],
'type':'live',
'title':'[COMBROS] Джордж Бруйвик - All of this #9',
'viewer_count':1326,
'started_at':'2018-01-02T05:36:07Z',
'language':'en',
'thumbnail_url':'https://static-cdn.jtvnw.net/previews-ttv/live_user_igorghk-{width}x{height}.jpg'
},
{
'id':'27146956464',
'user_id':'68064974',
'game_id':'32982',
'community_ids':[
],
'type':'live',
'title':'HYVÄÄ UUTTAVUOTTA // 3.1.18 klo: 12-00 12h Livestream subien kunniaksi! // !subgiveaway',
'viewer_count':998,
'started_at':'2018-01-02T12:58:49Z',
'language':'en',
'thumbnail_url':'https://static-cdn.jtvnw.net/previews-ttv/live_user_mrtuomostream-{width}x{height}.jpg'
},
{
'id':'27147162448',
'user_id':'58529158',
'game_id':'32982',
'community_ids':[
'5181e78f-2280-42a6-873d-758e25a7c313',
'a61f2599-ba45-4391-905c-ca0c8cbf61a6',
'ff1e77af-551d-4993-945c-f8ceaa2a2829'
],
'type':'live',
'title':'( English ) Happy New Year | Kaitlyn Maw',
'viewer_count':781,
'started_at':'2018-01-02T13:36:32Z',
'language':'en',
'thumbnail_url':'https://static-cdn.jtvnw.net/previews-ttv/live_user_katie-{width}x{height}.jpg'
},
{
'id':'27147842992',
'user_id':'69759951',
'game_id':'32982',
'community_ids':[
],
'type':'live',
'title':'GTARP - Kudo Kai - NoPixel',
'viewer_count':312,
'started_at':'2018-01-02T15:24:50Z',
'language':'en',
'thumbnail_url':'https://static-cdn.jtvnw.net/previews-ttv/live_user_vader-{width}x{height}.jpg'
},
{
'id':'27144250400',
'user_id':'3345556',
'game_id':'32982',
'community_ids':[
'ab18bbee-75ed-409d-aba9-8d4e3e2e541a'
],
答案 0 :(得分:1)
对于一个子字符串:
[x['user_id'] for x in data['data'] if 'rp' in x['title'].lower()]
对于多个子字符串:
[x['user_id'] for x in data['data'] if any(s in x.get('title', '').lower() for s in ['hey', 'hi', 'rp'])]
由于这变得复杂,这是它的正常迭代版本:
ids = []
parts = ['hey', 'hi', 'rp']
for user in data['data']:
title = user.get('title', '').lower()
if any(part in title for part in parts):
ids.append(user['user_id'])
答案 1 :(得分:1)
我会在上面添加此评论作为对Omar评论的评论,但我没有代表。
当它回到示例JSON中的最后一个条目时,该答案将抛出异常,因为没有'title'
条目。
你需要做
[x['user_id'] for x in data['data'] if 'rp' in x.get('title', '').lower()]
答案 2 :(得分:0)
尝试使用' if'在您现有的代码中
[x ['user_id'] for x in data['data'] if 'rp' in x['title']]
答案 3 :(得分:0)
我想使用过滤方法:
一线解决方案:
print(list(filter(lambda x:'rp' in x['title'].lower(),data['data'])))
输出:
[{'user_id': '69759951', 'community_ids': [], 'title': 'GTARP - Kudo Kai - NoPixel', 'thumbnail_url': 'https://static-cdn.jtvnw.net/previews-ttv/live_user_vader-{width}x{height}.jpg', 'viewer_count': 312, 'started_at': '2018-01-02T15:24:50Z', 'game_id': '32982', 'language': 'en', 'type': 'live', 'id': '27147842992'}]
当数据是:
data={
'data':[
{
'id':'27144974352',
'user_id':'23308993',
'game_id':'32982',
'community_ids':[
'af543777-83ec-402d-8245-30da1409e22a',
'c0294643-085c-4943-94ab-f0e1ba090e23',
'd74020e4-53af-48c3-b3d1-f23500a490c8'
],
'type':'live',
'title':'[COMBROS] Джордж Бруйвик - All of this #9',
'viewer_count':1326,
'started_at':'2018-01-02T05:36:07Z',
'language':'en',
'thumbnail_url':'https://static-cdn.jtvnw.net/previews-ttv/live_user_igorghk-{width}x{height}.jpg'
},
{
'id':'27146956464',
'user_id':'68064974',
'game_id':'32982',
'community_ids':[
],
'type':'live',
'title':'HYVÄÄ UUTTAVUOTTA // 3.1.18 klo: 12-00 12h Livestream subien kunniaksi! // !subgiveaway',
'viewer_count':998,
'started_at':'2018-01-02T12:58:49Z',
'language':'en',
'thumbnail_url':'https://static-cdn.jtvnw.net/previews-ttv/live_user_mrtuomostream-{width}x{height}.jpg'
},
{
'id':'27147162448',
'user_id':'58529158',
'game_id':'32982',
'community_ids':[
'5181e78f-2280-42a6-873d-758e25a7c313',
'a61f2599-ba45-4391-905c-ca0c8cbf61a6',
'ff1e77af-551d-4993-945c-f8ceaa2a2829'
],
'type':'live',
'title':'( English ) Happy New Year | Kaitlyn Maw',
'viewer_count':781,
'started_at':'2018-01-02T13:36:32Z',
'language':'en',
'thumbnail_url':'https://static-cdn.jtvnw.net/previews-ttv/live_user_katie-{width}x{height}.jpg'
},
{
'id':'27147842992',
'user_id':'69759951',
'game_id':'32982',
'community_ids':[
],
'type':'live',
'title':'GTARP - Kudo Kai - NoPixel',
'viewer_count':312,
'started_at':'2018-01-02T15:24:50Z',
'language':'en',
'thumbnail_url':'https://static-cdn.jtvnw.net/previews-ttv/live_user_vader-{width}x{height}.jpg'
},
]
}