[Numpy-discussion] initializing an array of lists

2009-11-07 Thread alan
I want to build a 2D array of lists, and so I need to initialize the
array with empty lists :

myarray = array([[[],[],[]] ,[[],[],[]]])

Is there a clever way to do this? I could define the array

myarray = zeros( (xdim,ydim), dtype=object)
and then iterate through the elements initializing then to empty lists, but 
surely there is a better way.

-- 
---
| Alan K. Jackson| To see a World in a Grain of Sand  |
| a...@ajackson.org  | And a Heaven in a Wild Flower, |
| www.ajackson.org   | Hold Infinity in the palm of your hand |
| Houston, Texas | And Eternity in an hour. - Blake   |
---
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] initializing an array of lists

2009-11-07 Thread Alan G Isaac
On 11/7/2009 10:56 PM, a...@ajackson.org wrote:
> I want to build a 2D array of lists, and so I need to initialize the
> array with empty lists :
>
> myarray = array([[[],[],[]] ,[[],[],[]]])


[[[] for i in range(3)] for j in range(2) ]

fwiw,
Alan Isaac

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] initializing an array of lists

2009-11-08 Thread alan
>On 11/7/2009 10:56 PM, a...@ajackson.org wrote:
>> I want to build a 2D array of lists, and so I need to initialize the
>> array with empty lists :
>>
>> myarray = array([[[],[],[]] ,[[],[],[]]])  
>
>
>[[[] for i in range(3)] for j in range(2) ]
>
>fwiw,
>Alan Isaac

Thanks! I like that - concise and a little strange. 8-)


-- 
---
| Alan K. Jackson| To see a World in a Grain of Sand  |
| a...@ajackson.org  | And a Heaven in a Wild Flower, |
| www.ajackson.org   | Hold Infinity in the palm of your hand |
| Houston, Texas | And Eternity in an hour. - Blake   |
---
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] initializing an array of lists

2009-11-08 Thread Ravi
On Saturday 07 November 2009 22:56:29 a...@ajackson.org wrote:
> I want to build a 2D array of lists, and so I need to initialize the
> array with empty lists :
> 
> myarray = array([[[],[],[]] ,[[],[],[]]])


In [1]: [[[]]*3]*2
Out[1]: [[[], [], []], [[], [], []]]

Hope this helps.

Ravi

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] initializing an array of lists

2009-11-08 Thread Keith Goodman
On Sun, Nov 8, 2009 at 5:02 PM, Ravi  wrote:
> On Saturday 07 November 2009 22:56:29 a...@ajackson.org wrote:
>> I want to build a 2D array of lists, and so I need to initialize the
>> array with empty lists :
>>
>> myarray = array([[[],[],[]] ,[[],[],[]]])
>
>
> In [1]: [[[]]*3]*2
> Out[1]: [[[], [], []], [[], [], []]]

Have to be careful with that one:

>> x = [[[]]*3]*2
>> x[0][0] = []
>> x
   [[[], [], []], [[], [], []]]

but not with this one:

>> x = [[[] for i in range(3)] for j in range(2)]
>> x[0][0] = []
>> x
   [[[], [], []], [[], [], []]]
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] initializing an array of lists

2009-11-09 Thread Christopher Barker
a...@ajackson.org wrote:
> myarray = zeros( (xdim,ydim), dtype=object)
> and then iterate through the elements initializing then to empty lists, but 
> surely there is a better way.

I tried this:

In [3]: a = np.empty((2,3), dtype=np.object)

In [5]: a[:,:] = []

but got:

ValueError: shape mismatch: objects cannot be broadcast to a single shape

Is that a bug? Or is it simply too ambiguous for numpy to figure out 
what the heck I want?

-Chris




-- 
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R(206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115   (206) 526-6317   main reception

chris.bar...@noaa.gov
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] initializing an array of lists

2009-11-09 Thread Robert Kern
On Mon, Nov 9, 2009 at 12:00, Christopher Barker  wrote:
> a...@ajackson.org wrote:
>> myarray = zeros( (xdim,ydim), dtype=object)
>> and then iterate through the elements initializing then to empty lists, but
>> surely there is a better way.
>
> I tried this:
>
> In [3]: a = np.empty((2,3), dtype=np.object)
>
> In [5]: a[:,:] = []
>
> but got:
>
> ValueError: shape mismatch: objects cannot be broadcast to a single shape
>
> Is that a bug? Or is it simply too ambiguous for numpy to figure out
> what the heck I want?

The latter.

In any case, you wouldn't want each element to be the same list object.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless
enigma that is made terrible by our own mad attempt to interpret it as
though it had an underlying truth."
  -- Umberto Eco
___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] initializing an array of lists

2009-11-09 Thread Pauli Virtanen
Sat, 07 Nov 2009 21:56:29 -0600, alan wrote:
> I want to build a 2D array of lists, and so I need to initialize the
> array with empty lists :
> 
> myarray = array([[[],[],[]] ,[[],[],[]]])
> 
> Is there a clever way to do this? I could define the array
> 
> myarray = zeros( (xdim,ydim), dtype=object) and then iterate through the
> elements initializing then to empty lists, but surely there is a better
> way.

This question seems to come up from time to time (= maybe should be a 
FAQ?). You can for example vectorize the list constructor:

>>> filler = np.frompyfunc(lambda x: list(), 1, 1)
>>> a = np.empty((3, 4), dtype=np.object)
>>> filler(a, a);
array([[[], [], [], []],
   [[], [], [], []],
   [[], [], [], []]], dtype=object)
>>> a[0,3].append(9)
>>> a
array([[[], [], [], [9]],
   [[], [], [], []],
   [[], [], [], []]], dtype=object)

-- 
Pauli Virtanen

___
NumPy-Discussion mailing list
NumPy-Discussion@scipy.org
http://mail.scipy.org/mailman/listinfo/numpy-discussion