Question related to this post.
I need help to filter a list of lists depending on a variable set of conditions.
Here is an extract of the list:
ex = [
# ["ref", "type", "date"]
[1, 'CB', '2017-12-11'],
[2, 'CB', '2017-12-01'],
[3, 'RET', '2017-11-08'],
[1, 'CB', '2017-11-08'],
[5, 'RET', '2017-10-10'],
]
I want to apply a final treatment like list(filter(makeCondition, ex)) where makeCondition(myList, **kwargs) function returns a boolean that enables to filter the list.
I have troubles building this function as adviced in this post, as the number of conditions defined in the **kwargs dictionnary is variable.
Examples of conditions sets:
conditions = {"ref": 3}
conditions = {"ref": 1, "type": "CB"}
Here is a start:
def makeConditions(myList, **p):
# For each key:value in the dictionnary
for key, value in p.items():
if key == "ref":
lambda x: x[0] == value
elif key == "type":
lambda x: x[1] == value
elif key == "date":
lambda x: x[2] == value
# return chained conditions...
list(filter(makeConditions, ex))
I don't get the logical idea that various comments attempted to give in the post mentioned above... Do I have to execute the filter() function for each sublist or is it possible to do it for the whole global list? Any advices will be very welcome!
conditionsvariable is variable indeed, as shown in the post. The content of the sublists is always constant with the 3 objects: ref, type and date.