python - Filter list using Boolean index arrays -


how can use boolean inddex arrays filter list without using numpy?

for example:

>>> l = ['a','b','c'] >>> b = [true,false,false] >>> l[b] 

the result should be:

['a'] 

i know numpy support want know how solve in python.

>>> import numpy np >>> l = np.array(['a','b','c']) >>> b = np.array([true,false,false]) >>> l[b] array(['a'],        dtype='|s1') 

python not support boolean indexing itertools.compress function want. return iterator means need use list constructor return list.

>>> itertools import compress >>> l = ['a', 'b', 'c'] >>> b = [true, false, false] >>> list(compress(l, b)) ['a'] 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -