[Numpy-discussion] Array assignment problem

Anne Archibald peridot.faceted@gmail....
Tue Mar 11 18:13:12 CDT 2008


On 11/03/2008, Dinesh B Vadhia <dineshbvadhia@hotmail.com> wrote:

> Hello!  I'm reading a text file with two numbers in str format on each line.
>  The numbers are converted into integers.  Each integer is then assigned to
> a 2-dimensional array ij (see code below).  The problem is that neither of
> the array assignments work ie. both ij[index, 0] = r and ij[index, 1] = c
> are always 0 (zero).  I've checked r and c and both are integers (>=0).
>
> import sys
> import os
> import numpy
>
> nnz = 1200000
> ij = numpy.array(numpy.empty((nnz, 2), dtype=int))
> index = 0
> filename = 'test_ij.txt'
> for line in open(filename, 'r'):
>     line = line.rstrip('\n')
>     r, c = map(str, line.split(','))
>     r = int(r)
>     c = int(c)
>     ij[index, 0] = r
>     ij[index, 1] = c
>     index = index + 1
>
>
> What am I doing wrong?

The first thing you're doing wrong is you're not using numpy.loadtxt:
In [35]: numpy.loadtxt('foo',delimiter=",",dtype=numpy.int)
Out[35]:
array([[1, 3],
       [4, 5],
       [6, 6]])
This removes the need for the rest of your code. To find useful
functions like this in future, you can try looking at
http://www.scipy.org/Numpy_Functions_by_Category

Stripping the newline off is unnecessary, since int("  17  \n")==17.
Also, since the results of line.split(,) are already strings, the
map(str, ...) doesn't do anything. Did you mean it to?

Otherwise, your code works fine for me.

I should point out that using empty(), you should expect your array to
be full of gibberish (rather than 0), so if you're seeing lots of
zeros, they're probably coming from the text file.

Good luck,
Anne


More information about the Numpy-discussion mailing list