How do I make a grid of empty lists in numpy that 'accepts' appending?

后端 未结 3 976
南笙
南笙 2021-01-24 16:09

I am trying to use numpy.append but something goes wrong and it just doesn\'t make sence to me anymore. Can someone explain why I am getting an error?

>>&g         


        
3条回答
  •  Happy的楠姐
    2021-01-24 16:31

    First of all, stop updating your question when someone answers it correctly. Accept/upvote an answer and ask a new question when you are ready. SO is a Q&A site that is meant to help future visitors viewing your question, and not just you. Invalidating all the good answers by changing the context completely for your own benefit defeats the purpose of this site, to say the least.

    Secondly, np.array([[], [3], []]) ends up with dtype=object because it is a ragged array. [[], [], []] and [[3], [3], [3]], having a uniform length in all elements across all dimensions, would produce numerical arrays.

    np.zeros((6,),dtype=object) produces an empty array of object references (containing NULLs). When you fill it with [], you are filling it with a reference to the same python list in every element. numpy has no knowledge of what object you are passing to ndarray.fill, so it does not call the constructor on the list type for each element as you seem to be expecting. It just copies the reference that you passed in six times. After that, it should be clear why changing the contents of that one list makes it appear that all the array elements have changed.

提交回复
热议问题