First post from a Python newbiw

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

    First post from a Python newbiw

    I finally decided to have a go with Python and am working through the
    tutorial.

    On my old BBC Computer I could do something like this:

    DIM A(2,2)

    to create a 3 by 3 array of data. Then I could set any point:

    A(0,0) = foo
    A(0,1) = bar
    etc.

    In Python I thought I could do this with:
    >>a=[0,0,0]
    >>b=[a,a,a]
    >>b
    [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    >>b[1][1]='foo'
    >>b
    [[0, 'foo', 0], [0, 'foo', 0], [0, 'foo', 0]]
    >>>
    I can understand why as b[1][1]='foo' is actually changing a[1]

    Apart from doing something like
    a=[0,0,0]
    b=[0,0,0]
    c=[0,0,0]
    d=[a,b,c]

    is there a better way of creating d??

    --
    Steve


  • Marc 'BlackJack' Rintsch

    #2
    Re: First post from a Python newbiw

    On Sun, 02 Mar 2008 14:15:09 +0000, Steve Turner wrote:
    Apart from doing something like
    a=[0,0,0]
    b=[0,0,0]
    c=[0,0,0]
    d=[a,b,c]
    >
    is there a better way of creating d??
    a = [[0] * 3 for dummy in xrange(3)]

    Ciao,
    Marc 'BlackJack' Rintsch

    Comment

    • Steve Turner

      #3
      Re: First post from a Python newbiw

      Marc 'BlackJack' Rintsch wrote:

      : On Sun, 02 Mar 2008 14:15:09 +0000, Steve Turner wrote:
      :
      :: Apart from doing something like
      :: a=[0,0,0]
      :: b=[0,0,0]
      :: c=[0,0,0]
      :: d=[a,b,c]
      ::
      :: is there a better way of creating d??
      :
      : a = [[0] * 3 for dummy in xrange(3)]

      Thanks, Marc.

      --
      Steve

      Comment

      • Christoph Zwerschke

        #4
        Re: First post from a Python newbiw

        Marc 'BlackJack' Rintsch schrieb:
        On Sun, 02 Mar 2008 14:15:09 +0000, Steve Turner wrote:
        >
        >Apart from doing something like
        >a=[0,0,0]
        >b=[0,0,0]
        >c=[0,0,0]
        >d=[a,b,c]
        >>
        >is there a better way of creating d??
        >
        a = [[0] * 3 for dummy in xrange(3)]
        Why not simply [[0]*3]*3 ?

        -- Christoph

        Comment

        • Steve Turner

          #5
          Re: First post from a Python newbiw

          Christoph Zwerschke wrote:

          : Marc 'BlackJack' Rintsch schrieb:
          :: On Sun, 02 Mar 2008 14:15:09 +0000, Steve Turner wrote:
          ::
          ::: Apart from doing something like
          ::: a=[0,0,0]
          ::: b=[0,0,0]
          ::: c=[0,0,0]
          ::: d=[a,b,c]
          :::
          ::: is there a better way of creating d??
          ::
          :: a = [[0] * 3 for dummy in xrange(3)]
          :
          : Why not simply [[0]*3]*3 ?

          I've just tried that and it gives the same as my earlier b=[a,a,a]

          --
          Steve

          Comment

          • Marc 'BlackJack' Rintsch

            #6
            Re: First post from a Python newbiw

            On Sun, 02 Mar 2008 21:58:31 +0100, Christoph Zwerschke wrote:
            Marc 'BlackJack' Rintsch schrieb:
            >On Sun, 02 Mar 2008 14:15:09 +0000, Steve Turner wrote:
            >>
            >>Apart from doing something like
            >>a=[0,0,0]
            >>b=[0,0,0]
            >>c=[0,0,0]
            >>d=[a,b,c]
            >>>
            >>is there a better way of creating d??
            >>
            >a = [[0] * 3 for dummy in xrange(3)]
            >
            Why not simply [[0]*3]*3 ?
            Because:

            In [77]: a = [[0] * 3] * 3

            In [78]: a
            Out[78]: [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

            In [79]: a[0][0] = 42

            In [80]: a
            Out[80]: [[42, 0, 0], [42, 0, 0], [42, 0, 0]]

            Ciao,
            Marc 'BlackJack' Rintsch

            Comment

            • Terry Reedy

              #7
              Re: First post from a Python newbiw


              "Christoph Zwerschke" <cito@online.de wrote in message
              news:fqf4do$gq7 $1@online.de...
              | Marc 'BlackJack' Rintsch schrieb:
              | On Sun, 02 Mar 2008 14:15:09 +0000, Steve Turner wrote:
              | >
              | >Apart from doing something like
              | >a=[0,0,0]
              | >b=[0,0,0]
              | >c=[0,0,0]
              | >d=[a,b,c]
              | >>
              | >is there a better way of creating d??
              | >
              | a = [[0] * 3 for dummy in xrange(3)]
              |
              | Why not simply [[0]*3]*3 ?

              Because that is essentially the same as what the OP originally did,
              which does not work as he wanted.



              Comment

              • Jeff Schwab

                #8
                Re: First post from a Python newbiw

                Christoph Zwerschke wrote:
                Marc 'BlackJack' Rintsch schrieb:
                >On Sun, 02 Mar 2008 14:15:09 +0000, Steve Turner wrote:
                >>
                >>Apart from doing something like
                >>a=[0,0,0]
                >>b=[0,0,0]
                >>c=[0,0,0]
                >>d=[a,b,c]
                >>>
                >>is there a better way of creating d??
                >>
                >a = [[0] * 3 for dummy in xrange(3)]
                Each element of a refers to a distinct array.
                Why not simply [[0]*3]*3 ?
                All three elements of the result refer to the same array.

                Comment

                • Arnaud Delobelle

                  #9
                  Re: First post from a Python newbiw



                  Steve Turner wrote:
                  I finally decided to have a go with Python and am working through the
                  tutorial.
                  Great!
                  On my old BBC Computer [...]
                  These were nice machines...
                  In Python I thought I could do this with:
                  >
                  >a=[0,0,0]
                  >b=[a,a,a]
                  >b
                  [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
                  >b[1][1]='foo'
                  >b
                  [[0, 'foo', 0], [0, 'foo', 0], [0, 'foo', 0]]
                  >>
                  >
                  I can understand why as b[1][1]='foo' is actually changing a[1]
                  >
                  Apart from doing something like
                  a=[0,0,0]
                  b=[0,0,0]
                  c=[0,0,0]
                  d=[a,b,c]
                  >
                  is there a better way of creating d??
                  It's a FAQ:
                  Contents: Programming FAQ- General Questions- Is there a source code level debugger with breakpoints, single-stepping, etc.?, Are there tools to help find bugs or perform static analysis?, How can ...


                  --
                  Arnaud

                  Comment

                  • Christoph Zwerschke

                    #10
                    Re: First post from a Python newbiw

                    Arnaud Delobelle schrieb:Somewhere on my todo list I have "read through the whole Python FAQ",
                    but so far never got round doing it. Should probably set it to prio A.

                    -- Christoph

                    Comment

                    Working...