numeric module

S

shama.bell

Hello,
What's the problem with this code? I get the following error message:

File "test.py", line 26, in test
print tbl[wi][bi]
IndexError: index must be either an int or a sequence

---code snippet----

from Numeric import *
tbl = zeros((32, 16))

def test():

val = testme()
wi = crc >> 4
bi = crc & 0xFL
print wi
print bi
print tbl[wi][bi]


def testme():
val = 0xFFFFFFL
val >>= 23
return val


if __name__ == '__main__':
test()

Thanks,
-SB
 
C

coffeebug

I don't know much here...but is there an assumption you're making about
the machine word size to be greather than 24 bits?
Also, more to the point, does the function zeros() instantiate a
two-dimensional table? If so, does it populate the table with any
values? The error looks like tbl[ ][ ] doesn't get defined.
Thanks for patience!
 
S

Steven Bethard

Hello,
What's the problem with this code? I get the following error message:

File "test.py", line 26, in test
print tbl[wi][bi]
IndexError: index must be either an int or a sequence

---code snippet----

from Numeric import *
tbl = zeros((32, 16))

def test():

val = testme()
wi = crc >> 4
bi = crc & 0xFL
print wi
print bi
print tbl[wi][bi]


def testme():
val = 0xFFFFFFL
val >>= 23
return val


if __name__ == '__main__':
test()

Hmm... I can't reproduce this with numarray. Maybe this only exists in
Numeric? Are you sure that the code here is what produced the error?

py> import numarray as na
py> tbl = na.zeros((32, 16))
py> def test():
.... val = testme()
.... wi = val >> 4
.... bi = val & 0xFL
.... print wi
.... print bi
.... print tbl[wi][bi]
....
py> def testme():
.... val = 0xFFFFFFL
.... val >>= 23
.... return val
....
py> test()
0
1
0

STeVe
 
S

shama.bell

Thanks Steve.

There's a problem with Numeric array. I tried with numarray and it
works fine.

-SB
 
C

coffeebug

I cannot import "numarray" and I cannot import "numeric" using python
2.3.3

Where would I find an equivalent definition for zeros()?

Anyway, is there supposed to be something that sets the value of
elements of tbl to values other than zero? Not that the question has
anything to do with Shama's problem (recognizing tbl as a
two-dimensional array).
 
T

Terry Reedy

What's the problem with this code? I get the following error message:

2.4 or earlier?
File "test.py", line 26, in test
print tbl[wi][bi]
IndexError: index must be either an int or a sequence

Longs are not ints; message implies that Numeric, unlike Python, does not
allow them as indexes.
from Numeric import *
tbl = zeros((32, 16))

def test():

val = testme()

val is never used
wi = crc >> 4

crc is never set, so how did this run?
bi = crc & 0xFL

making bi a long. Try 0xF which should be faster anyway.
print wi
print bi print type(wi), type(bi)
print tbl[wi][bi]

def testme():
val = 0xFFFFFFL
val >>= 23
return val

if __name__ == '__main__':
test()
 
D

David M. Cooke

Hello,
What's the problem with this code? I get the following error message:

File "test.py", line 26, in test
print tbl[wi][bi]
IndexError: index must be either an int or a sequence

---code snippet----

from Numeric import *
tbl = zeros((32, 16))

def test():

val = testme()
wi = val >> 4
bi = val & 0xFL
[above changed to use val instead of crc, as you mentioned in another post]
print wi
print bi
print tbl[wi][bi]

tbl[wi][bi] would be indexing the bi'th element of whatever tbl[wi]
returns. For Numeric arrays, you need

tbl[wi,bi]

Now, you'll have another problem as Terry Reedy mentioned: the indices
(in Numeric) need to be Python ints, not longs. You could rewrite your
test() function as

def test():
val = testme()
wi = int(val >> 4)
bi = int(val & 0xF)
print wi
print bi
print tbl[wi,bi]

and that'll work.
 

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,774
Messages
2,569,596
Members
45,143
Latest member
DewittMill
Top