[Numpy-discussion] array comprehension

Neal Becker ndbecker2 at gmail.com
Fri Nov 4 11:56:39 EDT 2016


Francesc Alted wrote:

> 2016-11-04 14:36 GMT+01:00 Neal Becker <ndbecker2 at gmail.com>:
> 
>> Francesc Alted wrote:
>>
>> > 2016-11-04 13:06 GMT+01:00 Neal Becker <ndbecker2 at gmail.com>:
>> >
>> >> I find I often write:
>> >> np.array ([some list comprehension])
>> >>
>> >> mainly because list comprehensions are just so sweet.
>> >>
>> >> But I imagine this isn't particularly efficient.
>> >>
>> >
>> > Right.  Using a generator and np.fromiter() will avoid the creation of
>> the
>> > intermediate list.  Something like:
>> >
>> > np.fromiter((i for i in range(x)))  # use xrange for Python 2
>> >
>> >
>> Does this generalize to >1 dimensions?
>>
> 
> A reshape() is not enough?  What do you want to do exactly?
> 

I was thinking about:
x = np.array ([[L1] L2]) where L1,L2 take the form of a list comprehension,
as a means to create a 2-D array (in this example)




More information about the NumPy-Discussion mailing list