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.

Tad
  • 838
  • 2
  • 11
  • 22
  • Without knowing the dimensions of the array there is no way to allocate memory for it. You should look at [this question](http://stackoverflow.com/questions/5064822/how-to-add-items-into-a-numpy-array) for some details. – Ffisegydd Aug 18 '14 at 14:32
  • 1
    Do you mean you don't know the dimensions when you write the code, or when you run it? – jonrsharpe Aug 18 '14 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. – Joe Kington Aug 18 '14 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! – Tad Aug 18 '14 at 15:50
  • 1
    @Tad but in that case could you use e.g. [`numpy.loadtxt`](http://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html) to load the file into an array? Then you don't have to explicitly set the size. – jonrsharpe Aug 18 '14 at 15:52
  • Thanks @jonrsharpe This looks promising. By the way, how can you open that link? It never loads for me or any of my friends. I wonder what might cause this. – Tad Aug 18 '14 at 16:20
  • Looks like http://docs.scipy.org is down - try [the Google cache](http://webcache.googleusercontent.com/search?q=cache:1XdWQmblflEJ:docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html+&cd=1&hl=en&ct=clnk&gl=uk) – jonrsharpe Aug 18 '14 at 16:22

0 Answers0