[Numpy-discussion] when and where to use numpy arrays vs nested lists

Mark P. Miller mpmusu@cc.usu....
Thu Mar 1 11:38:47 CST 2007

>> ##imports
>> import numpy as NP
>>from numpy.random import randint
>> #numpy array code
>> array1 = NP.zeros((50,50), int)
>> def random1():
>>     c = array1(randint(10), randint(10))
> Is this a bug?  You can't "call" an array.  Did you mean, 
> array1[randint(10), randint(10)]?

Good catch...I accidentally cut and pasted a line containing a typo.  I 
actually used c = array1[randint(50), randint(50)] when doing my 

> Getting single indices like this is a bit slower for NumPy then for 
> lists because of all the possibilities that must be distinguished for 
> array indexing.   List indexing is a very simple thing.  Thus, lists can 
> be seen as collections that are optimized for simple indexing.  If all 
> you are doing is simple indexing, then lists are going to be faster.
> You can try using array1.item(randint(10), randint(10)), like this:
> getitem = array1.item
> def random1():
>       getitem(randint(50), randint(50))
> Also, you might try using
> array2 = array1.tolist()
> instead of creating one like you do. 
> I get comparable speeds in this case.

Excellent...I'll check on this.  It appears that the simplest approach 
is not the best in this case.  Thanks for pointing this out.


> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion@scipy.org
> http://projects.scipy.org/mailman/listinfo/numpy-discussion

More information about the Numpy-discussion mailing list