在具有不同键顺序的不同列表中搜索键,值
Search for a key,value in different lists which has different key orders
我正在使用 tweepy 检索 json.I 中的链接,已经提取了一个包含字典的列表。它们中的大多数都采用相同的模式(键和值的顺序相同),如下所示
[{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://A_LINK'},
{'bitrate': 632000, 'content_type': 'video/mp4', 'url': 'https://B_LINK'},
{'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'},
{'bitrate': 832000, 'content_type': 'video/mp4', 'url': 'https://D_LINK'}]
以上模式有两个特点:
1.{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://A_LINK'}
在 {'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
的前面
2.{'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
缺少密钥 比特率
我想找到比特率为2176000的url(即https://A_LINK[=例如 63=]),下面的代码 working 上面的模式我可以找到 https://A_LINK
link = next((item for item in x if item["bitrate"] == 2176000), None)
print(link["url"])
但是,由于 {'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
缺少密钥 Bitrate
会导致以下模式出错。
[{'bitrate': 632000, 'content_type': 'video/mp4', 'url': 'https://A_LINK'},
{'bitrate': 832000, 'content_type': 'video/mp4', 'url': 'https://B_LINK'},
{'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'},
{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://D_LINK'}]
对于上述模式,
{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://D_LINK'}
位于 'content_type': 'application/x-mpegURL'
之后
每当我收到这样的模式时,我都会收到错误消息
KeyError: 'bitrate'
我无法在 {'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
之后获取 https://D_LINK
因此我问,当我收到第二个列表模式时,
有没有所谓的“绕过”的方法{'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
这样我就可以得到 https://D_LINK 最后比特率 2176000?
谢谢。
这是您要找的吗?
l = [
{'bitrate': 632000, 'content_type': 'video/mp4', 'url': 'https://A_LINK'},
{'bitrate': 832000, 'content_type': 'video/mp4', 'url': 'https://B_LINK'},
{'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'},
{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://D_LINK'},
]
''.join([d["url"] for d in l if "bitrate" in d.keys() and d['bitrate'] == 2176000])
输出:
'https://D_LINK'
我正在使用 tweepy 检索 json.I 中的链接,已经提取了一个包含字典的列表。它们中的大多数都采用相同的模式(键和值的顺序相同),如下所示
[{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://A_LINK'},
{'bitrate': 632000, 'content_type': 'video/mp4', 'url': 'https://B_LINK'},
{'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'},
{'bitrate': 832000, 'content_type': 'video/mp4', 'url': 'https://D_LINK'}]
以上模式有两个特点:
1.{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://A_LINK'}
在 {'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
2.{'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
缺少密钥 比特率
我想找到比特率为2176000的url(即https://A_LINK[=例如 63=]),下面的代码 working 上面的模式我可以找到 https://A_LINK
link = next((item for item in x if item["bitrate"] == 2176000), None)
print(link["url"])
但是,由于 {'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
缺少密钥 Bitrate
会导致以下模式出错。
[{'bitrate': 632000, 'content_type': 'video/mp4', 'url': 'https://A_LINK'},
{'bitrate': 832000, 'content_type': 'video/mp4', 'url': 'https://B_LINK'},
{'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'},
{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://D_LINK'}]
对于上述模式,
{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://D_LINK'}
位于 'content_type': 'application/x-mpegURL'
每当我收到这样的模式时,我都会收到错误消息
KeyError: 'bitrate'
我无法在 {'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
因此我问,当我收到第二个列表模式时,
有没有所谓的“绕过”的方法{'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'}
这样我就可以得到 https://D_LINK 最后比特率 2176000?
谢谢。
这是您要找的吗?
l = [
{'bitrate': 632000, 'content_type': 'video/mp4', 'url': 'https://A_LINK'},
{'bitrate': 832000, 'content_type': 'video/mp4', 'url': 'https://B_LINK'},
{'content_type': 'application/x-mpegURL', 'url': 'https://C_LINK'},
{'bitrate': 2176000, 'content_type': 'video/mp4', 'url': 'https://D_LINK'},
]
''.join([d["url"] for d in l if "bitrate" in d.keys() and d['bitrate'] == 2176000])
输出:
'https://D_LINK'