Efficiently generate numpy array from list comprehension output?

Posted by shootingstars on Stack Overflow See other posts from Stack Overflow or by shootingstars
Published on 2012-12-14T10:25:17Z Indexed on 2012/12/14 11:04 UTC
Read the original article Hit count: 126

Filed under:
|

Is there a more efficient way than using numpy.asarray() to generate an array from output in the form of a list?

This appears to be copying everything in memory, which doesn't seem like it would be that efficient with very large arrays.

(Updated) Example:

import numpy as np
a1 = np.array([1,2,3,4,5,6,7,8,9,10]) # pretend this has thousands of elements
a2 = np.array([3,7,8])

results = np.asarray([np.amax(np.where(a1 > element)) for element in a2])

© Stack Overflow or respective owner

Related posts about python

Related posts about numpy