Need help initializing a list or tuple.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • KraftDiner

    #1

    Need help initializing a list or tuple.

    I need a matrix of 256 x 256 unsigned short values...
    The matrix can be implemented as a list of lists or a tuple...
    So for example:
    [[1][2][3], [4][5][6], [7][8][9]]
    or
    ((1,2,3),(4,5,6 ), (7,8,9))

    This is what I have so far but its not working...

    a = [][]
    for i in range(0,256):
    for j in range(0,256):
    a[i][j] = i**2

  • Fredrik Lundh

    #2
    Re: Need help initializing a list or tuple.

    "KraftDiner " wrote:
    [color=blue]
    > I need a matrix of 256 x 256 unsigned short values...
    > The matrix can be implemented as a list of lists or a tuple...
    > So for example:
    > [[1][2][3], [4][5][6], [7][8][9]]
    > or
    > ((1,2,3),(4,5,6 ), (7,8,9))
    >
    > This is what I have so far but its not working...
    >
    > a = [][]
    > for i in range(0,256):
    > for j in range(0,256):
    > a[i][j] = i**2[/color]

    this might help:

    Contents: Programming FAQ- General questions- Is there a source code-level debugger with breakpoints and single-stepping?, Are there tools to help find bugs or perform static analysis?, How can I c...


    </F>



    Comment

    • KraftDiner

      #3
      Re: Need help initializing a list or tuple.

      Thank you that worked great!

      a = [[None] * 256] * 256
      for i in range(0,256):
      for j in range(0,256):
      a[i][j] = i**2

      Now.. Can I change this quickly into a 1 dimensional list of length
      65536?

      Comment

      • Diez B. Roggisch

        #4
        Re: Need help initializing a list or tuple.

        KraftDiner schrieb:[color=blue]
        > Thank you that worked great!
        >
        > a = [[None] * 256] * 256
        > for i in range(0,256):
        > for j in range(0,256):
        > a[i][j] = i**2
        >
        > Now.. Can I change this quickly into a 1 dimensional list of length
        > 65536?[/color]

        sum(a, [])

        Diez


        Comment

        • KraftDiner

          #5
          Re: Need help initializing a list or tuple.

          Strange behaviour...

          a = [[None] * 256] * 256
          for i in range(0,256):
          for j in range(0,256):
          a[i][j] = i**2
          a = sum(a, [])
          print a[0]
          print a[65535]

          Prints:

          65025
          65025

          Comment

          • Fredrik Lundh

            #6
            Re: Need help initializing a list or tuple.

            "KraftDiner " wrote:
            [color=blue]
            > Strange behaviour...
            >
            > a = [[None] * 256] * 256
            > for i in range(0,256):
            > for j in range(0,256):
            > a[i][j] = i**2
            > a = sum(a, [])
            > print a[0]
            > print a[65535]
            >
            > Prints:
            >
            > 65025
            > 65025[/color]

            looks like you missed that the page I pointed you to explained *why*
            the a = [[None] * 256] * 256 approach doesn't work...

            </F>



            Comment

            • KraftDiner

              #7
              Re: Need help initializing a list or tuple.

              Yes I missed the fine print that said that the code had 'side effects'
              So:

              a = [None]*256
              for i in range(256):
              a[i] = [None] * 256
              for i in range(0,256):
              for j in range(0,256):
              a[i][j] = i**2

              a = sum(a, [])
              print a[0]
              print a[65535]

              Comment

              • John Salerno

                #8
                Re: Need help initializing a list or tuple.

                KraftDiner wrote:
                [color=blue]
                > [[1][2][3], [4][5][6], [7][8][9]][/color]

                I'm confused. Is that a valid list?

                Comment

                • KraftDiner

                  #9
                  Re: Need help initializing a list or tuple.

                  Oh by the way the b = sum(a, []) worked well to convert the two
                  dimensional array to a 1D array...
                  Is there a way to bring that 1D array back to a 2D array?

                  Comment

                  • KraftDiner

                    #10
                    Re: Need help initializing a list or tuple.

                    nope.. sorry its not...
                    I meant x = [[1,2,3],[3,4,5],[5,6,7]]

                    Comment

                    • Blackbird

                      #11
                      Re: Need help initializing a list or tuple.

                      John Salerno wrote:[color=blue]
                      > KraftDiner wrote:
                      >[color=green]
                      >> [[1][2][3], [4][5][6], [7][8][9]][/color]
                      >
                      > I'm confused. Is that a valid list?[/color]

                      No. I'm assuming he meant [[1, 2, 3], [4, 5, 6], [7, 8, 9]].


                      Comment

                      • John Salerno

                        #12
                        Re: Need help initializing a list or tuple.

                        KraftDiner wrote:[color=blue]
                        > nope.. sorry its not...
                        > I meant x = [[1,2,3],[3,4,5],[5,6,7]]
                        >[/color]

                        Whew, you were about to really throw me for a loop otherwise! :)

                        Comment

                        • BranoZ

                          #13
                          Re: Need help initializing a list or tuple.

                          How about:

                          a = [ [i**2 for j in range(256)] for i in range(256) ]
                          b = sum(a, [])
                          c = [ b[slice(i,i+256)] for i in range(0,256*256 ,256) ][color=blue][color=green][color=darkred]
                          >>> a is c[/color][/color][/color]
                          False[color=blue][color=green][color=darkred]
                          >>> a == c[/color][/color][/color]
                          True

                          BranoZ

                          Comment

                          • cdunn2001@gmail.com

                            #14
                            Re: Need help initializing a list or tuple.

                            # Nested list comprehensions act like real for loops:[color=blue][color=green][color=darkred]
                            >>> a = [[None for i in range(3)] for j in range(3)]
                            >>> a[/color][/color][/color]
                            [[None, None, None], [None, None, None], [None, None, None]][color=blue][color=green][color=darkred]
                            >>> a[0][0] = 5
                            >>> a[/color][/color][/color]
                            [[5, None, None], [None, None, None], [None, None, None]]
                            # Side-effect: i and j will be changed.

                            # Another way (much less clear to my eyes):[color=blue][color=green][color=darkred]
                            >>> a = map(lambda x: map(lambda x: None, range(3)), range(3))
                            >>> a[0][0]=5
                            >>> a[/color][/color][/color]
                            [[5, None, None], [None, None, None], [None, None, None]]

                            Comment

                            • Terry Reedy

                              #15
                              Re: Need help initializing a list or tuple.


                              "KraftDiner " <bobrien18@yaho o.com> wrote in message
                              news:1141666188 .237150.320690@ j52g2000cwj.goo glegroups.com.. .[color=blue]
                              > Thank you that worked great!
                              >
                              > a = [[None] * 256] * 256
                              > for i in range(0,256):
                              > for j in range(0,256):
                              > a[i][j] = i**2
                              >
                              > Now.. Can I change this quickly into a 1 dimensional list of length
                              > 65536?[/color]

                              If 'a' were a Numeric/Numarray/NumPy array, then I believe you could
                              re-dimensionalize it on the fly to be 1x65536 instead of 256x256. See
                              numeric.scipy.o rg for more.

                              tjr



                              Comment

                              Working...