By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,706 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,706 IT Pros & Developers. It's quick & easy.

numeric module

P: n/a
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

Jul 18 '05 #1
Share this Question
Share on Google+
9 Replies


P: n/a
In function test(),
Read wi and bi as:
wi = val >> 4
bi = val & 0xFL

-SB

Jul 18 '05 #2

P: n/a
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!

Jul 18 '05 #3

P: n/a
The table initializes to a 2 dimensional with zeros.
-SB

Jul 18 '05 #4

P: n/a
sh********@gmail.com wrote:
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
Jul 18 '05 #5

P: n/a
Thanks Steve.

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

-SB

Jul 18 '05 #6

P: n/a
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).

Jul 18 '05 #7

P: n/a

<sh********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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()


Jul 18 '05 #8

P: n/a
sh********@gmail.com writes:
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.

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #9

P: n/a
"coffeebug" <fr****@netscape.net> writes:
I cannot import "numarray" and I cannot import "numeric" using python
2.3.3


numarray and Numeric are separate modules available at
http://numpy.sourceforge.net/

If you're doing anything numerical in Python, you'll want them :-)

--
|>|\/|<
/--------------------------------------------------------------------------\
|David M. Cooke
|cookedm(at)physics(dot)mcmaster(dot)ca
Jul 18 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.