I have two lists of lists, one that includes all records e.g. [['eggs', 'milk', 'butter'], ['ham', 'spam', 'milk'], ['cereal', 'skittles']] and one that contains rules [['milk', 'eggs'], ['milk','ham']].
I'm trying to filter records by the list_of_rules, however, I want to capture [['eggs', 'milk', 'butter'], ['ham', 'spam', 'milk']] despite it not exactly matching [['milk', 'eggs'], ['milk','ham']] order and extra items wise.
records = [['eggs', 'milk', 'butter'], ['ham', 'spam', 'milk'], ['cereal', 'skittles']]
list_of_rules = [['milk', 'eggs'], ['milk','ham']]
# this list comprehension only filters for exact matches
results = [[x for x in L if x in records] for L in list_of_rules]
# expected output
print(results)
>>[['eggs', 'milk', 'butter'], ['ham', 'spam', 'milk']]
Any and all recommendations very much appreciated.