Nested Lists Assignment Problem

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

    #1

    Nested Lists Assignment Problem

    I wanna use nested lists as an array, but here's the problem:
    [color=blue][color=green][color=darkred]
    >>> a = [[0]*3]*3
    >>> a[/color][/color][/color]
    [[0, 0, 0], [0, 0, 0], [0, 0, 0]][color=blue][color=green][color=darkred]
    >>> a[0][0] = 1
    >>> a[/color][/color][/color]
    [[1, 0, 0], [1, 0, 0], [1, 0, 0]]

    Could anybody please explain to me why three values were change? I'm
    bewildered. Thanks!

  • tiksin

    #2
    Re: Nested Lists Assignment Problem

    Le Mercredi 26 Avril 2006 10:13, Licheng Fang a écrit :[color=blue]
    > I wanna use nested lists as an array, but here's the problem:[color=green][color=darkred]
    > >>> a = [[0]*3]*3
    > >>> a[/color][/color]
    >
    > [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
    >[color=green][color=darkred]
    > >>> a[0][0] = 1
    > >>> a[/color][/color]
    >
    > [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
    >
    > Could anybody please explain to me why three values were change? I'm
    > bewildered. Thanks![/color]

    I guess is a probleme of reference. Write that instead:
    [color=blue][color=green][color=darkred]
    >>>l = [ [0]*3 for i in xrange(3) ][/color][/color][/color]

    cordialement,

    Comment

    • Licheng Fang

      #3
      Re: Nested Lists Assignment Problem

      Dennis Lee Bieber wrote:[color=blue]
      > On 26 Apr 2006 01:13:20 -0700, "Licheng Fang" <fanglicheng@gm ail.com>
      > declaimed the following in comp.lang.pytho n:
      >
      >[color=green]
      > >
      > > Could anybody please explain to me why three values were change? I'm
      > > bewildered. Thanks![/color]
      >
      > http://www.aifb.uni-karlsruhe.de/Leh...20FAQ.htm#4.50
      > --[color=green]
      > > =============== =============== =============== =============== == <
      > > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
      > > wulfraed@dm.net | Bestiaria Support Staff <
      > > =============== =============== =============== =============== == <
      > > Home Page: <http://www.dm.net/~wulfraed/> <
      > > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color][/color]

      Thank you very much!

      But I still wonder why a nested assignment "a = [[0]*3]*3" generates 3
      references to the same list, while the commands below apparently do
      not.
      [color=blue][color=green][color=darkred]
      >>> a = [0] * 2
      >>> a[/color][/color][/color]
      [0, 0][color=blue][color=green][color=darkred]
      >>> a[0] = 1
      >>> a[/color][/color][/color]
      [1, 0]

      Comment

      • Fredrik Lundh

        #4
        Re: Nested Lists Assignment Problem

        "Licheng Fang" wrote:
        [color=blue]
        >I wanna use nested lists as an array, but here's the problem:
        >[color=green][color=darkred]
        >>>> a = [[0]*3]*3
        >>>> a[/color][/color]
        > [[0, 0, 0], [0, 0, 0], [0, 0, 0]][color=green][color=darkred]
        >>>> a[0][0] = 1
        >>>> a[/color][/color]
        > [[1, 0, 0], [1, 0, 0], [1, 0, 0]]
        >
        > Could anybody please explain to me why three values were change? I'm
        > bewildered. Thanks![/color]



        </F>



        Comment

        • Fredrik Lundh

          #5
          Re: Nested Lists Assignment Problem

          "Licheng Fang" wrote:
          [color=blue]
          > But I still wonder why a nested assignment "a = [[0]*3]*3" generates 3
          > references to the same list, while the commands below apparently do
          > not.[/color]

          that's because they're replacing a list item, rather than modifying it.
          [color=blue][color=green][color=darkred]
          >>>> a = [0] * 2
          >>>> a[/color][/color]
          > [0, 0] <- now you have two references to the same integer.[color=green][color=darkred]
          >>>> a[0] = 1 <- now you've *replaced* the first integer with another integer.
          >>>> a[/color][/color]
          > [1, 0][/color]

          if you do the same thing with lists, it behaves in exactly the same way:
          [color=blue][color=green][color=darkred]
          >>> a = [[0]*3]*3
          >>> a[/color][/color][/color]
          [[0, 0, 0], [0, 0, 0], [0, 0, 0]] <-- three references to the same list[color=blue][color=green][color=darkred]
          >>> a[0] = [1, 2, 3] <-- replace the first list
          >>> a[/color][/color][/color]
          [[1, 2, 3], [0, 0, 0], [0, 0, 0]]

          however, if you modify the *shared* object, the modification will of course
          be visible everywhere that object is used:
          [color=blue][color=green][color=darkred]
          >>> a[1][0] = "hello" <-- modified the first item in the shared list
          >>> a[/color][/color][/color]
          [[1, 2, 3], ['hello', 0, 0], ['hello', 0, 0]]

          </F>



          Comment

          • Deepan Chakravarthy

            #6
            pyumlgraph for multiple files.

            Hello,
            I had used pyumlgraph to generate dot files for single python script. Is
            it possible to use pyumlgraph to generate dot files for multiple python
            scripts ??? Or is it automatically done if i generate a dot file for the
            final python script that imports all other files?

            Thanks
            Deepan Chakravarthy N
            Great prices on a large selection of domains. Find the pefect domain for your new startup.

            Comment

            • Gary Herron

              #7
              Re: Nested Lists Assignment Problem

              Licheng Fang wrote:
              [color=blue]
              >Dennis Lee Bieber wrote:
              >
              >[color=green]
              >>On 26 Apr 2006 01:13:20 -0700, "Licheng Fang" <fanglicheng@gm ail.com>
              >>declaimed the following in comp.lang.pytho n:
              >>
              >>
              >>
              >>[color=darkred]
              >>>Could anybody please explain to me why three values were change? I'm
              >>>bewildered . Thanks!
              >>>
              >>>[/color]
              >>http://www.aifb.uni-karlsruhe.de/Leh...20FAQ.htm#4.50
              >>--[color=darkred]
              >> > =============== =============== =============== =============== == <
              >> > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
              >> > wulfraed@dm.net | Bestiaria Support Staff <
              >> > =============== =============== =============== =============== == <
              >> > Home Page: <http://www.dm.net/~wulfraed/> <
              >> > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]
              >>
              >>[/color]
              >
              >Thank you very much!
              >
              >But I still wonder why a nested assignment "a = [[0]*3]*3" generates 3
              >references to the same list, while the commands below apparently do
              >not.
              >
              >
              >[/color]
              It's got nothing to do with "nested"-ness. If X is ANY object in
              Python, then [X]*3 will make a list with three references to X. If
              you don't want three references to the single object X (and you don't),
              then you have to find a different way to create three separate objects.
              The copy module helps with this in some cases, but for your simple
              example, you just want to create the three inner objects by evaluating
              the expression [0]*3 three times. Here's several ways:

              a = []
              for i in range(3):
              a.append([0]*3)

              or

              a = [ [0]*3 for i in range(3)]

              Clear?

              Gary Herron


              Comment

              Working...