Given a list and a bitmask, how do I return the values at the indices that are True?
By : Giuliano Berrettaros
Date : March 29 2020, 07:55 AM
I wish this help you Python 3.1 itertools.compress (or Python 2.7's if you haven't upgraded yet) does exactly that (the list comprehension is a real close second): code :
import itertools
filtered = itertools.compress(s, b)

Getting indices of True values in a boolean list
By : Attanox
Date : March 29 2020, 07:55 AM
it should still fix some issue I have a piece of my code where I'm supposed to create a switchboard. I want to return a list of all the switches that are on. Here "on" will equal True and "off" equal False. So now I just want to return a list of all the True values and their position. This is all I have but it only return the position of the first occurrence of True (this is just a portion of my code): , Use enumerate, list.index returns the index of first match found. code :
>>> t = [False, False, False, False, True, True, False, True, False, False, False, False, False, False, False, False]
>>> [i for i, x in enumerate(t) if x]
[4, 5, 7]
>>> from itertools import compress
>>> list(compress(xrange(len(t)), t))
[4, 5, 7]
>>> t = t*1000
>>> %timeit [i for i, x in enumerate(t) if x]
100 loops, best of 3: 2.55 ms per loop
>>> %timeit list(compress(xrange(len(t)), t))
1000 loops, best of 3: 696 µs per loop

Fill a boolean array with List that contains indices that must be true
By : user2165904
Date : March 29 2020, 07:55 AM
This might help you Suppose I have List NullValuesIds containing indices. code :
int maxIndex = NullValuesIds.Max(); // or some other maximum
var boolArray =
Enumerable
.Range(0, maxIndex)
.Select(NullValuesIds.Contains)
.ToArray();

Javascript  Get indices of true values in a boolean array
By : m.gorczyca
Date : March 29 2020, 07:55 AM
Hope this helps You can use .reduce() to do this in one pass: code :
const arr = [true, true, false, false, false, true, false]
const indices = arr.reduce(
(out, bool, index) => bool ? out.concat(index) : out,
[]
)
console.log(indices)
const arr = [true, true, false, false, false, true, false]
const indices = arr.flatMap((bool, index) => bool ? index : [])
console.log(indices)

Getting a list of indices where pandas boolean series is True
By : Mike R
Date : March 29 2020, 07:55 AM
I wish this helpful for you Using Boolean Indexing code :
>>> s = pd.Series([True, False, True, True, False, False, False, True])
>>> s[s].index
Int64Index([0, 2, 3, 7], dtype='int64')
>>> s[s].index.values
array([0, 2, 3, 7])
>>> np.nonzero(s)
(array([0, 2, 3, 7]),)
>>> np.flatnonzero(s)
array([0, 2, 3, 7])
>>> np.where(s)[0]
array([0, 2, 3, 7])
>>> np.argwhere(s).ravel()
array([0, 2, 3, 7])
>>> s.index[s]
array([0, 2, 3, 7])
>>> [*filter(s.get, s.index)]
[0, 2, 3, 7]
>>> [i for i in s.index if s[I]]
[0, 2, 3, 7]

