array of array of float

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

    #1

    array of array of float

    i used C too much and haven't used Python for a while...

    like in C, if we want an array of array of float, we use

    float a[200][500];

    now in Python, seems like we have to do something like

    a = [ [ ] ] * 200

    and then just use

    a[1].append(12.34) etc

    but it turns out that all 200 elements points to the same list...
    and i have to use

    a = [ ]
    for i in range (0, 200):
    a.append([ ])

    is there a simpler way... i wonder...

  • Fredrik Lundh

    #2
    Re: array of array of float

    Summercoolness@ gmail.com wrote:
    i used C too much and haven't used Python for a while...
    >
    like in C, if we want an array of array of float, we use
    >
    float a[200][500];
    >
    now in Python, seems like we have to do something like
    >
    a = [ [ ] ] * 200
    >
    and then just use
    >
    a[1].append(12.34) etc
    >
    but it turns out that all 200 elements points to the same list...
    and i have to use
    >
    a = [ ]
    for i in range (0, 200):
    a.append([ ])
    >
    is there a simpler way... i wonder...
    this is FAQ:



    </F>

    Comment

    • DH

      #3
      Re: array of array of float

      Summercoolness@ gmail.com wrote:
      i used C too much and haven't used Python for a while...
      >
      like in C, if we want an array of array of float, we use
      >
      float a[200][500];
      >
      now in Python, seems like we have to do something like
      >
      a = [ [ ] ] * 200
      >
      and then just use
      >
      a[1].append(12.34) etc
      >
      but it turns out that all 200 elements points to the same list...
      and i have to use
      >
      a = [ ]
      for i in range (0, 200):
      a.append([ ])
      >
      is there a simpler way... i wonder...
      >
      Right, try the numpy module, and you can do:

      from numpy import *
      a = zeros((200,500) , Float)

      documentation:

      download:
      http://sourceforge.net/project/showf...kage_id=175103
      main page:

      Comment

      • Schüle Daniel

        #4
        Re: array of array of float

        Summercoolness@ gmail.com schrieb:
        i used C too much and haven't used Python for a while...
        >
        like in C, if we want an array of array of float, we use
        >
        float a[200][500];
        >
        now in Python, seems like we have to do something like
        >
        a = [ [ ] ] * 200
        >
        and then just use
        >
        a[1].append(12.34) etc
        >
        but it turns out that all 200 elements points to the same list...
        and i have to use
        >
        a = [ ]
        for i in range (0, 200):
        a.append([ ])
        >
        is there a simpler way... i wonder...
        a = [[] for in range(200)]

        Comment

        • Schüle Daniel

          #5
          Re: array of array of float

          Schüle Daniel schrieb:
          Summercoolness@ gmail.com schrieb:
          >i used C too much and haven't used Python for a while...
          >>
          >like in C, if we want an array of array of float, we use
          >>
          >float a[200][500];
          >>
          >now in Python, seems like we have to do something like
          >>
          >a = [ [ ] ] * 200
          >>
          >and then just use
          >>
          >a[1].append(12.34) etc
          >>
          >but it turns out that all 200 elements points to the same list...
          >and i have to use
          >>
          >a = [ ]
          >for i in range (0, 200):
          > a.append([ ])
          >>
          >is there a simpler way... i wonder...
          >
          a = [[] for in range(200)]
          correction :)

          a = [[] for i in range(200)]

          Comment

          • tac-tics

            #6
            Re: array of array of float

            Use nested list comprehensions:

            matrix = [[0.0 for x in xrange(n)] for y in xrange(m)]

            This is similar to "float matrix[m][n]" in C.

            All cells are independent of each other in doing this.

            Comment

            • Fredrik Lundh

              #7
              Re: array of array of float

              "Schüle Daniel" wrote:
              a = [[] for in range(200)]
              >
              correction :)
              >
              a = [[] for i in range(200)]
              the "*500" part still seems to be missing...

              </F>



              Comment

              Working...