python - Better way to vectorize using numpy -


i wondering if there better way vectorize following:

array = [] ele in long_list:     if condition:         array.append(1) vector = np.array(array) 

now np.ones(len(long_list)) except if condition.

is there better (vectorized) way of achieving above?

vector = np.fromiter((1 ele in long_list if condition), dtype=int) 

this should faster either:

vector = np.ones(len([x x in long_list if conditon])) 

or

vector = np.array( [1 ele in long_list if condition(ele)] ) 

because avoids building filtered list, due fact (1 ele in long_list if condition) returns generator not list.

edit

i'm wondering if isn't viable option:

vector = np.array( (1 ele in long_list if condition(ele)) ) 

Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -