[Numpy-discussion] initializing an array of lists

Keith Goodman kwgoodman at gmail.com
Sun Nov 8 20:33:22 EST 2009


On Sun, Nov 8, 2009 at 5:02 PM, Ravi <lists_ravi at lavabit.com> wrote:
> On Saturday 07 November 2009 22:56:29 alan at 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] = [9999]
>> x
   [[[9999], [], []], [[9999], [], []]]

but not with this one:

>> x = [[[] for i in range(3)] for j in range(2)]
>> x[0][0] = [9999]
>> x
   [[[9999], [], []], [[], [], []]]



More information about the NumPy-Discussion mailing list