I need to use the following syntax to filter the list operations:
a = [ope for ope in operations if ope[0] == 1]
The if statement condition is variable and may contain multiple conditions:
a = [ope for ope in operations if ope[0] == 1 and ope[1] == "test"]
I use a function to build the condition and return it as a string:
>>>> c = makeCondition(**{"id": 1, "title": 'test'})
>>>> c
"ope[0] == 1 and ope[1] == 'test'"
Is there a way to integrate the c variable into the list filtering? Something like this (of course, the c variable is evaluated as a string in the below example):
a = [ope for ope in operations if c]
Thanks for help!
解决方案
As commented, if you want to change the string to be considered as an expression, you can use eval(string).