469,925 Members | 1,493 Online

returning a list: IndexError

Hello,

I am getting the following error when returning a list:

return tbl[c1,c2]

"IndexError: each subindex must be either a slice, an integer,
Ellipsis, or NewAxis"

What does it mean?

Here's the code snippet....

from Numeric import *

# Initialize the 32x16 global array to zeros
tbl = zeros((32, 16)

def getValue( value):
data = test(value)
c1 = data
c2 = data
print tbl[c1, c2]

def test( value):
t1 = 0x0
t2 = 0x1
return tbl[t1, t2]

Thanks,
-SB

Jul 18 '05 #1
5 1566 sh********@gmail.com wrote:
from Numeric import *

# Initialize the 32x16 global array to zeros
tbl = zeros((32, 16)

def getValue( value):
data = test(value)
c1 = data
c2 = data
print tbl[c1, c2]

def test( value):
t1 = 0x0
t2 = 0x1
return tbl[t1, t2]

In test, tbl[0x0, 0x1] is just going to give you a single element of
tbl, in this case a 0. So data is a 0. data and data doesn't
really make much sense. Is this the code you actually get the
IndexError with? I'm using numarray, not Numeric, and I know there are
some differences, but the code above doesn't give me an IndexError:

py> import numarray as na
py> tbl = na.zeros((32, 16))
py> def get_value():
.... data = test()
.... c1 = data
.... c2 = data
.... print tbl[c1, c2]
....
py> def test():
.... t1 = 0x0
.... t2 = 0x1
.... return tbl[t1, t2]
....
py> get_value()
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 3, in get_value
TypeError: unsubscriptable object

STeVe
Jul 18 '05 #2

Steven Bethard wrote:
sh********@gmail.com wrote:
from Numeric import *

# Initialize the 32x16 global array to zeros
tbl = zeros((32, 16)

def getValue( value):
data = test(value)
c1 = data
c2 = data
print tbl[c1, c2]

def test( value):
t1 = 0x0
t2 = 0x1
return tbl[t1, t2]
In test, tbl[0x0, 0x1] is just going to give you a single element of
tbl, in this case a 0. So data is a 0. data and data doesn't
really make much sense. Is this the code you actually get the
IndexError with? I'm using numarray, not Numeric, and I know there

are some differences, but the code above doesn't give me an IndexError:

py> import numarray as na
py> tbl = na.zeros((32, 16))
py> def get_value():
... data = test()
... c1 = data
... c2 = data
... print tbl[c1, c2]
...
py> def test():
... t1 = 0x0
... t2 = 0x1
... return tbl[t1, t2]
...
py> get_value()
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 3, in get_value
TypeError: unsubscriptable object

STeVe

What i was intending to do is return a list with row and column values.

data and data are the list values i get from test.

Whats the best way to read these row and column values in function
get_value() when i return the list (return tbl[t1, t2])

Thanks,
-SB

Jul 18 '05 #3
sh********@gmail.com wrote:
Steven Bethard wrote:

py> import numarray as na
py> tbl = na.zeros((32, 16))
py> def get_value():
... data = test()
... c1 = data
... c2 = data
... print tbl[c1, c2]
...
py> def test():
... t1 = 0x0
... t2 = 0x1
... return tbl[t1, t2]
...
py> get_value()
Traceback (most recent call last):
File "<interactive input>", line 1, in ?
File "<interactive input>", line 3, in get_value
TypeError: unsubscriptable object

What i was intending to do is return a list with row and column values.

data and data are the list values i get from test.

Whats the best way to read these row and column values in function
get_value() when i return the list (return tbl[t1, t2])

Still not sure I understand you. Are you wanting c1 and c2 to retrieve
the values that t1 and t2 had? (That is, 0x0 and 0x1?) Or do you want
c1 and c2 to hold the first row and the first column? Or something else?

STeVe
Jul 18 '05 #4
Thats right. I wanted c1 and c2 to retrieve the values returned by t1
and t2 . Values for t1 and t2 could be anything. Also tbl is global.

-SB

Jul 18 '05 #5
sh********@gmail.com wrote:
Thats right. I wanted c1 and c2 to retrieve the values returned by t1
and t2 . Values for t1 and t2 could be anything. Also tbl is global.

Then you need to return t1 and t2 in test, e.g.:

py> import numarray as na
py> tbl = na.zeros((32, 16))
py> def test():
.... t1 = 0x0
.... t2 = 0x1
.... return t1, t2, tbl[t1, t2]
....
py> def get_value():
.... c1, c2, data = test()
.... print tbl[c1, c2]
....
py> get_value()
0

Just as an element of any other container doesn't know what index it's
stored at, values in a numarray array don't either. If you want to deal
with indices, you need to either pass them around instead (or in
addition to) the values, or you need to search the whole array each time
for similar objects and see what indices they're at. The first approach
is probably preferred in most cases.

STeVe
Jul 18 '05 #6

 6 posts views Thread by Ishwor | last post: by 13 posts views Thread by TB | last post: by 35 posts views Thread by erikwickstrom | last post: by reply views Thread by DarrenWeber | last post: by reply views Thread by eddparker01 | last post: by reply views Thread by lanliddd | last post: by 1 post views Thread by isladogs | last post: by reply views Thread by Trystan | last post: by reply views Thread by Trystan | last post: by 9 posts views Thread by anoble1 | last post: by 1 post views Thread by skydivetom | last post: by reply views Thread by WIPE | last post: by reply views Thread by MikeCant | last post: by