returning a list: IndexError

S

shama.bell

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[0]
c2 = data[1]
print tbl[c1, c2]

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

Thanks,
-SB
 
S

Steven Bethard

from Numeric import *

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

def getValue( value):
data = test(value)
c1 = data[0]
c2 = data[1]
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[0] and data[1] 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[0]
.... c2 = data[1]
.... 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
 
S

shama.bell

Steven said:
from Numeric import *

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

def getValue( value):
data = test(value)
c1 = data[0]
c2 = data[1]
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[0] and data[1] 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[0]
... c2 = data[1]
... 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[0] and data[1] 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
 
S

Steven Bethard

Steven said:
py> import numarray as na
py> tbl = na.zeros((32, 16))
py> def get_value():
... data = test()
... c1 = data[0]
... c2 = data[1]
... 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[0] and data[1] 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
 
S

shama.bell

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
 
S

Steven Bethard

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top