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 5 1744 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
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 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
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 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ishwor |
last post by:
i am trying to remove an item 'e' from the list l but i keep getting IndexError.
I know the size of the list l is changing in the for loop & its sort
of trivial task but i found no other way than...
|
by: TB |
last post by:
Hi,
Is there an elegant way to assign to a list from a list of unknown
size? For example, how could you do something like:
>>> a, b, c = (line.split(':'))
if line could have less than three...
|
by: erikwickstrom |
last post by:
Hi all,
I'm sorry about the newbie question, but I've been searching all
afternoon and can't find the answer!
I'm trying to get this bit of code to work without triggering the
IndexError.
...
|
by: DarrenWeber |
last post by:
# Copyright (C) 2007 Darren Lee Weber
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |