469,925 Members | 1,493 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,925 developers. It's quick & easy.

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[0]
c2 = data[1]
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[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
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[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

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

This discussion thread is closed

Replies have been disabled for this discussion.

By using this site, you agree to our Privacy Policy and Terms of Use.