[英]python filter function on a list of dictionaries
suppose that I have following result from the api :假设我从 api 得到以下结果:
[
{
"id":"1234",
"views":132624,
"rate":"4.43",
"url":"someurl.com",
"added":"2022-06-14 16:27:28",
"default_thumb":{
"size":"medium",
"width":640,
"height":360,
}
},
{
"id":"1234",
"views":132624,
"rate":"4.43",
"url":"someurl.com",
"added":"2022-06-14 16:27:28",
"default_thumb":{
"size":"medium",
"width":640,
"height":360,
}
},
...
]
and I just want to fetch urls in dictionaries, to do that I tried to filter the list with python filter()
function :我只想在字典中获取 url,为此我尝试使用 python filter()
函数过滤列表:
fetched_urls = list(filter(lambda video_data: video_data['url'] , videos_data))
but when I print fetched_urls
I'll get all of the array without any filter process, is there any way to achieve this filtered array using filter()
function ?但是当我打印fetched_urls
时,我将在没有任何过滤过程的情况下获得所有数组,有没有办法使用filter()
函数来实现这个过滤数组?
您需要map
而不是filter
filter returns the items if the condition is met.如果满足条件,过滤器将返回项目。 try this:尝试这个:
[a['url'] for a in video_data if 'url' in a]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.