[Numpy-discussion] Array assignment problem
Tue Mar 11 18:13:12 CDT 2008
On 11/03/2008, Dinesh B Vadhia <email@example.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 : numpy.loadtxt('foo',delimiter=",",dtype=numpy.int)
This removes the need for the rest of your code. To find useful
functions like this in future, you can try looking at
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.
More information about the Numpy-discussion