filter a array in a list that contain a certain value
Question:
I have list
combi_col = [[0,1,2][0,1],[0],[1,2],[0,2],[1,2,3],[2,3],[3,1]
I want to filter this list to only show a list that only contain 0 in the array
The result should only keep all arrays in the list which contain 0
In addition, the array should not contain only 1 value. It should contain minimum 2 values
The result should look like
result = [[0,1,2][0,1],[0,2]]
Answers:
You can try something like this:
combi_col = [[0,1,2],[0,1],[0],[1,2],[0,2],[1,2,3],[2,3],[3,1]]
result = []
for entry in combi_col:
if 0 in entry and len(entry) >= 2:
result.append(entry)
You can do in one line via list comprehension:
[x for x in combi_col if 0 in x and len(x) >= 2]
#[[0, 1, 2], [0, 1], [0, 2]]
You could use built in filter:
filtered = list(filter(lambda list_element: 0 in list_element and len(list_element)>1, combi_col))
First parameter is function with which you filter, and second is collection to be filtered.
Answer from NiiRexo is correct, but you can do the same in one line using list comprehension:
result = [nested_list for nested_list in combi_col if len(nested_list)>1 and (0 in nested_list)]
To filter the list and get the desired result, you can use a list comprehension combined with the in operator to check if the element 0 is present in each sublist and the len function to check if the sublist has more than one element.
Here’s an example of how you could do this:
result = [x for x in combi_col if 0 in x and len(x) > 1]
This will create a new list called result that only contains the sublists from combi_col that satisfy both conditions: they contain the element 0 and they have more than one element. The resulting list will be:
result = [[0,1,2][0,1],[0,2]]
list = [[0,1,2],[0,1],[0],[1,2],[0,2],[1,2,3],[2,3],[3,1]]
result = []
for i in list:
if 0 in i and len(i) >= 2:
result.append(i)
I have list
combi_col = [[0,1,2][0,1],[0],[1,2],[0,2],[1,2,3],[2,3],[3,1]
I want to filter this list to only show a list that only contain 0 in the array
The result should only keep all arrays in the list which contain 0
In addition, the array should not contain only 1 value. It should contain minimum 2 values
The result should look like
result = [[0,1,2][0,1],[0,2]]
You can try something like this:
combi_col = [[0,1,2],[0,1],[0],[1,2],[0,2],[1,2,3],[2,3],[3,1]]
result = []
for entry in combi_col:
if 0 in entry and len(entry) >= 2:
result.append(entry)
You can do in one line via list comprehension:
[x for x in combi_col if 0 in x and len(x) >= 2]
#[[0, 1, 2], [0, 1], [0, 2]]
You could use built in filter:
filtered = list(filter(lambda list_element: 0 in list_element and len(list_element)>1, combi_col))
First parameter is function with which you filter, and second is collection to be filtered.
Answer from NiiRexo is correct, but you can do the same in one line using list comprehension:
result = [nested_list for nested_list in combi_col if len(nested_list)>1 and (0 in nested_list)]
To filter the list and get the desired result, you can use a list comprehension combined with the in operator to check if the element 0 is present in each sublist and the len function to check if the sublist has more than one element.
Here’s an example of how you could do this:
result = [x for x in combi_col if 0 in x and len(x) > 1]
This will create a new list called result that only contains the sublists from combi_col that satisfy both conditions: they contain the element 0 and they have more than one element. The resulting list will be:
result = [[0,1,2][0,1],[0,2]]
list = [[0,1,2],[0,1],[0],[1,2],[0,2],[1,2,3],[2,3],[3,1]]
result = []
for i in list:
if 0 in i and len(i) >= 2:
result.append(i)