Filtering a list based on a list of booleans
You're looking for itertools.compress
:
>>> from itertools import compress
>>> list_a = [1, 2, 4, 6]
>>> fil = [True, False, True, False]
>>> list(compress(list_a, fil))
[1, 4]
Timing comparisons(py3.x):
>>> list_a = [1, 2, 4, 6]
>>> fil = [True, False, True, False]
>>> %timeit list(compress(list_a, fil))
100000 loops, best of 3: 2.58 us per loop
>>> %timeit [i for (i, v) in zip(list_a, fil) if v] #winner
100000 loops, best of 3: 1.98 us per loop
>>> list_a = [1, 2, 4, 6]*100
>>> fil = [True, False, True, False]*100
>>> %timeit list(compress(list_a, fil)) #winner
10000 loops, best of 3: 24.3 us per loop
>>> %timeit [i for (i, v) in zip(list_a, fil) if v]
10000 loops, best of 3: 82 us per loop
>>> list_a = [1, 2, 4, 6]*10000
>>> fil = [True, False, True, False]*10000
>>> %timeit list(compress(list_a, fil)) #winner
1000 loops, best of 3: 1.66 ms per loop
>>> %timeit [i for (i, v) in zip(list_a, fil) if v]
100 loops, best of 3: 7.65 ms per loop
Don't use filter
as a variable name, it is a built-in function.
Filter elements from list based on True/False from another list
You can use itertools.compress
:
>>> from itertools import compress
>>> a = [True, False, True, False]
>>> b = [2, 3, 5, 7]
>>> list(compress(b, a))
[2, 5]
Refer "itertools.compress()" document for more details
Using one list with boolean to filter another list
Extract with Map
.
Map(`[`, work.list, logical.list)
# $vector1
# [1] "a" "c"
#
# $vector2
# [1] "e"
Data:
logical.list) <- list(mask1 = c(TRUE, FALSE, TRUE), mask2 = c(FALSE, TRUE, FALSE
))
work.list <- list(vector1 = c("a", "b", "c"), vector2 = c("d", "e", "f"))
Filter list using Boolean index arrays
Python does not support boolean indexing but the itertools.compress
function does exactly what you want. It return an iterator with means you need to use the list
constructor to return a list.
>>> from itertools import compress
>>> l = ['a', 'b', 'c']
>>> b = [True, False, False]
>>> list(compress(l, b))
['a']
Filtering elements of one list by looking at boolean values from the second list
Not really. The cleanest way to do this is probably
List.map (fn (x,y) => x) (List.filter (fn (x,y) => y) (ListPair.zip (L1,L2)))
or
List.map Option.valOf (List.filter Option.isSome (ListPair.map(fn (x,y) => if y then SOME x else NONE) (L1,L2)))
The recursive function isn't too bad, either:
fun foo ([],[]) = []
| foo ([],L) = raise Fail "Different lengths"
| foo (L,[]) = raise Fail "Different lengths"
| foo (x::xs, b::bs) = if b then x::foo(xs,bs) else foo(xs,bs)
why my code is returning Boolean rather than showing the elements of the list when filtering based on date?
The 'where
method compares the entries and returns true\false for this comparison. By using .toList()
at the end as it is now in your code, you return the results of true\false
. now_2days returns a boolean, so I'm assuming you want to return the values where this whole logic results in true.
I would go for this:
filteredListbyDate = orderWidgets.where((element) => now_2days.isBefore(DateTime.parse(element.date))).toList();
Also, change this part of your code:
);
orderWidgets.add(orderWidgetshape);
}
filteredListbyDate = orderWidgets;
return Column(
children: filteredListbyDate,
Selection elements of a list based on another 'True'/'False' list
Use zip:
result = [x for x, y in zip(xs, ys) if y == 'True']
Example:
xs = ('sth1','sth2','sth3','sth4')
ys = ('True','False','True','False')
result = [x for x, y in zip(xs, ys) if y == 'True']
result
['sth1', 'sth3']
How to mask a list using boolean values from another list
You can use zip
and a list comprehension to perform a filter operation on y
based on corresponding truth values in x
:
x = [True, False, True, False]
y = ["a", "b", "c", "d"]
print([b for a, b in zip(x, y) if a])
Output:
['a', 'c']
itertools.compress
also does this:
>>> from itertools import compress
>>> x = [True, False, True, False]
>>> y = ["a", "b", "c", "d"]
>>> list(compress(y, x))
['a', 'c']
Related Topics
How to Interact with the Recaptcha Audio Element Using Selenium and Python
How to Remove Specific Tag/Sticker/Object from Images Using Opencv
Populate a Pandas Sparsedataframe from a Scipy Sparse Matrix
How to Efficiently Handle European Decimal Separators Using the Pandas Read_CSV Function
Finding Duplicate Files and Removing Them
Remove Non-Ascii Characters from Pandas Column
How to Read Datetime Back from SQLite as a Datetime Instead of String in Python
How to Efficiently Process a Numpy Array in Blocks Similar to Matlab's Blkproc (Blockproc) Function
Python - Using the Multiply Operator to Create Copies of Objects in Lists
How to Improve My Paw Detection
How to Read Contents of an Table in Ms-Word File Using Python
Why Do I Need to Deploy a "Default" App Before I Can Deploy Multiple Services in Gae
How to Enumerate a Range of Numbers Starting at 1
Drag and Drop Explorer Files to Tkinter Entry Widget