fill in 3D array

Robert Kern robert.kern at gmail.com
Thu Jul 24 03:51:34 CEST 2008


knielsen73 at gmail.com wrote:
> On Jul 23, 9:18 pm, Robert Kern <robert.k... at gmail.com> wrote:
>> knielse... at gmail.com wrote:
>>> Hi,
>>> I am a python newbie, trying to convert my IDL scripts to python. I am
>>> kind of stuck at the moment. I am reading in a 1-D data file with 2000
>>> data points. I need to put them in a 3-D array with size [10,10,20]. I
>>> have defined the field array as arr = zeros((10,10,20)) but don't know
>>> how to read the data into the array.
>> I assume that you are using numpy. Use numpy.fromfile() and the .reshape()
>> method. Assuming that your file is ASCII with numbers separated by whitespace:
>>
>>    import numpy
>>    arr = numpy.fromfile(thefilename, sep=' ').reshape((10,10,20))
>>
>> There is no need, in this case, to create an array before reading the data.
>>
>>> Also, I need to extract a slice of a 3-D array and tried a =
>>> array_name(:,:,20) but that didn't work.
>> Python uses [] brackets for indexing, not ().
>>
>>    arr[:,:,20]
> 
> Thanks, working like a charm.

Great. If you have more numpy questions, you will probably want to ask them on 
the numpy mailing list instead of here.

   http://www.scipy.org/Mailing_Lists

-- 
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




More information about the Python-list mailing list