initialize a dictionary

S

shama.bell

Hello,

Can I do something like this?

table = {}
table[32, 16] = 0x0

Where 32 specifies rows and 16 specifies columns and i am trying to
initialize it to zero

I should be able to do comparisons like:
table[t1, t2] == 0x1 etc.
-SB
 
S

Sean Blakey

Hello,

Can I do something like this?

table = {}
table[32, 16] = 0x0

Where 32 specifies rows and 16 specifies columns and i am trying to
initialize it to zero

I should be able to do comparisons like:
table[t1, t2] == 0x1 etc.
-SB

Try it. It works. Sort of.

This code actually creates a dict named "table" mapping the key tuple
(32, 16) to the value 0x0. Note that you are NOT creating a
two-dimensional array, so this approach may be problematic if you ever
need to iterate values "by row" or "by column".

There is a python F.A.Q. on this, which you may find useful:
http://www.python.org/doc/faq/programming.html#how-do-i-create-a-multidimensional-list
 
S

shama.bell

The F.A.Q. does not explain how to create a 2 dimensional array and
initialize it to zero.

I need to iterate values by row and column as well.

I tried this....
w,x = 32, 16
A = [ [0x0]*w for i in range(x)]
print A
It does not create a 2 dimensional array with 32 rows and 16 columns

Thanks,
-SB
 
S

Steven Bethard

I need to iterate values by row and column as well.

I tried this....
w,x = 32, 16
A = [ [0x0]*w for i in range(x)]
print A

py> import numarray
py> print numarray.zeros((16, 8))
[[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0]]

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