Nested List Filtering

I want to filter a nested list with another list, which can have a variable length. If any element of the sublist matches any element of the filter list, the sublist should be excluded. The following code works for me, but is there a “cleaner” solution for this task?

the_list = [['blue'], ['blue', 'red', 'black'], ['green', 'yellow'], ['yellow', 'green'], ['orange'], ['white', 'gray']]
filters = ['blue', 'white']

filtered_list = []
for sublist in the_list:
    for item in sublist:
        if item in filters:
            break
        filtered_list.append(sublist)
        break

Expected Result:

filtered_list = [['green', 'yellow'], ['yellow', 'green'], ['orange']]
+4
source share
7 answers

Perhaps a little more semantics will be used any.

for sublist in the_list:
    if any(item in filters_exclude for item in sublist):
        continue
    filtered_list.append(sublist)

It may be redundant, but you can even consider this in your own function, and then use the built-in filter

def good_list(some_list):
    return not any(item in filters_exclude for item in some_list)

filtered_list = filter(good_list, the_list)

This should fulfill the goal that you described. However, the code you wrote has potential problems, like mentioend in the comments.

+2
source

filter map, " ". , :

filters_exclude = [2, 4]
initial_list = [[1, 2, 3, 4], [1, 2, 3], [2, 3, 4, 5]]

final = list(map(lambda x: filter(lambda y: y not in filters_exclude, x), initial_list)

:

>>> filters_exclude = [2, 4]
>>> map(lambda x: filter(lambda y: y not in filters_exclude, x), [[1, 2, 3, 4], [1, 2, 3]])
[[1, 3], [1, 3]]
+2

:

the_list = [['blue'], ['blue', 'red', 'black'], ['green', 'yellow'],['orange'], ['white', 'gray']]
filters = ['blue', 'white']
final_l = [i for i in the_list if not any(b in filters for b in i)]

:

[['green', 'yellow'], ['orange']]

, :

final_l = filter(lambda x:not any(b in filters for b in x), the_list)
+2

You can use conditional list comprehension.

>>> [sublist for sublist in the_list 
     if all(filter not in set(sublist) for filter in filters)]
[['green', 'yellow'], ['orange']]
+2
source

You can also filter()lists that do not intersect with the list filtersusing Set intersection:

>>> the_list = [['blue'], ['blue', 'red', 'black'], ['green', 'yellow'], ['yellow', 'green'], ['orange'], ['white', 'gray']]
>>> filters = ['blue', 'white']
>>> list(filter(lambda x: not set(x).intersection(filters), the_list))
[['green', 'yellow'], ['yellow', 'green'], ['orange']]

Or with understanding:

>>> [x for x in the_list if not set(x).intersection(filters)]
[['green', 'yellow'], ['yellow', 'green'], ['orange']]
+2
source

Use of kits.

the_list = map(set, the_list)
filters = set(filters)

fl = []
for sub in the_list:
    sub = sub.difference(filters)
    if sub:
        fl.append(list(sub))
+2
source
filtered_list=[];
for sublist in the_list:
    if len(list(set(sublist).intersection(filters_exclude)))>0:
        break;
    filtered_list.append(sublist);

set (sublist) .intersection (filters_exclude) returns the intersection of both lists. List () converts a set to a list. Len () returns the length of the list.

0
source

Source: https://habr.com/ru/post/1692125/


All Articles