2

I am computing an expression which I know has dimensions of (200,200) in one case hence I can initialize the array with p = np.zeros((200,200))

what if I did not know the dimensions - is there any way to create such array without specifying the dimensions? Something like a list in java which can grow dynamically.

7
  • Without knowing the dimensions of the array there is no way to allocate memory for it. You should look at this question for some details. Commented Aug 18, 2014 at 14:32
  • 1
    Do you mean you don't know the dimensions when you write the code, or when you run it? Commented Aug 18, 2014 at 14:36
  • Numpy arrays aren't well suited to growing dynamically. (You can append to them, but you're creating a new copy and deleting the old one each time.) If you have something that needs to constantly change in size, use a list. In many cases you may even want to use a list of arrays (e.g. appending rows of a constant size). You can always convert to an array afterwards. Commented Aug 18, 2014 at 15:09
  • @jonrsharpe let's say I have p = some expression on data which I can load from a file. Different files can have different dimensions. For example, 200,200, or 500,200, etc. Hence I don't know what the dimensions will be. Ffisegydd Thank you for the link! Joe - thanks! Commented Aug 18, 2014 at 15:50
  • 1
    @Tad but in that case could you use e.g. numpy.loadtxt to load the file into an array? Then you don't have to explicitly set the size. Commented Aug 18, 2014 at 15:52

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.