initialize a dictionary

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • shama.bell@gmail.com

    #1

    initialize a dictionary

    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

  • Sean Blakey

    #2
    Re: initialize a dictionary

    On 30 Mar 2005 13:02:05 -0800, shama.bell@gmai l.com
    <shama.bell@gma il.com> wrote:[color=blue]
    > 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
    >[/color]

    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:




    --
    Sean Blakey
    Saint of Mild Amusement, Evil Genius, Big Geek
    Python/Java/C++/C(Unix/Windows/Palm/Web) developer
    quine = ['print "quine =",quine,"; exec(quine[0])"'] ; exec(quine[0])

    Comment

    • shama.bell@gmail.com

      #3
      Re: initialize a dictionary

      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

      Comment

      • Steven Bethard

        #4
        Re: initialize a dictionary

        shama.bell@gmai l.com wrote:[color=blue]
        > 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[/color]

        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

        Comment

        Working...