[英]calling a function from unqlite filter lambda
I need to perform filter using custom logic.我需要使用自定义逻辑执行过滤器。 I have defined a function as follows:我定义了一个 function 如下:
def isValid(str):
if str=='test':
true
else:
false
and calling it from a filter lambda as follows:并从过滤器 lambda 调用它,如下所示:
data.filter(lambda obj: isValid(obj['str'])
This doesn't work.这行不通。 What am I missing?我错过了什么?
If you requirement looks like this:如果您的要求如下所示:
data = [
{'name': "group1", "str":"test"},
{'name': "group2", "str":"validation"},
{'name': "group2", "str":"test"}
]
def isValid(str):
if str=='test':
return True
else:
return False
filtered_list = list(filter(lambda obj: isValid(obj['str']), data))
print(filtered_list)
output : [{'name': 'group1', 'str': 'test'}, {'name': 'group2', 'str': 'test'}]
###Note: object having test as value of key str is filtered
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.