Nested List Question

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

    #1

    Nested List Question

    Hello All,

    Could anyone tell me why this code produces the output it does?

    noAdjacencies = 2
    gridsPerAdj = 3
    rows = 4
    columns = 5


    gridSystemId = [[None]*columns]*rows
    for row in range(rows):
    for column in range(columns):
    gridSystemId[row][column] = "%d-%d" % (row,column)

    print gridSystemId

    Produces:

    [['3-0', '3-1', '3-2', '3-3', '3-4'], ['3-0', '3-1', '3-2', '3-3', '3-4'],
    ['3-0
    ', '3-1', '3-2', '3-3', '3-4'], ['3-0', '3-1', '3-2', '3-3', '3-4']]

    Rather than:

    [['0-0', '0-1', '0-2', '0-3', '0-4'], ['1-0', '1-1', '1-2', '1-3', '1-4'],
    ['2-0
    ', '2-1', '2-2', '2-3', '2-4'], ['3-0', '3-1', '3-2', '3-3', '3-4']]

    Thanks for your help,
    Chris M.



    ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
  • Alex Martelli

    #2
    Re: Nested List Question

    Newsfeeds <chris.mccoy@sp irentcom.com> wrote:
    [color=blue]
    > Hello All,
    >
    > Could anyone tell me why this code produces the output it does?[/color]
    ...[color=blue]
    > gridSystemId = [[None]*columns]*rows[/color]

    You've made gridSystemID a list of `rows` references to the SAME "inner"
    list, so the behavior you observe is the only possible one.

    If you want copies instead, ASK for copies...:

    gridSystemId = [ [None]*columns for x in xrange(rows) ]


    Alex

    Comment

    • Chris McCoy

      #3
      Re: Nested List Question

      Thank you! I've been banging my head against the wall!

      Chris M.

      "Alex Martelli" <aleax@mail.com cast.net> wrote in message
      news:1h5f4t2.1y v785h120xalfN%a leax@mail.comca st.net...[color=blue]
      > Newsfeeds <chris.mccoy@sp irentcom.com> wrote:
      >[color=green]
      >> Hello All,
      >>
      >> Could anyone tell me why this code produces the output it does?[/color]
      > ...[color=green]
      >> gridSystemId = [[None]*columns]*rows[/color]
      >
      > You've made gridSystemID a list of `rows` references to the SAME "inner"
      > list, so the behavior you observe is the only possible one.
      >
      > If you want copies instead, ASK for copies...:
      >
      > gridSystemId = [ [None]*columns for x in xrange(rows) ]
      >
      >
      > Alex
      >[/color]



      ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
      http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
      ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

      Comment

      • Roman Suzi

        #4
        Pychecker Re: Nested List Question

        On Thu, 3 Nov 2005, Chris McCoy wrote:
        [color=blue]
        > Thank you! I've been banging my head against the wall!
        >
        > Chris M.[/color]
        [color=blue][color=green]
        >> gridSystemId = [[None]*columns]*rows[/color]
        >
        > You've made gridSystemID a list of `rows` references to the SAME "inner"
        > list, so the behavior you observe is the only possible one.
        >
        > If you want copies instead, ASK for copies...:
        >
        > gridSystemId = [ [None]*columns for x in xrange(rows) ][/color]


        Interesting, could not pychecker recognize such situations in Python
        code and give warnings?


        Sincerely yours, Roman Suzi
        --
        rnd@onego.ru =\= My AI powered by GNU/Linux RedHat 7.3

        Comment

        • Chris McCoy

          #5
          Re: Pychecker Re: Nested List Question

          It may, but I haven't been using Pychecker yet. I'm still fairly new to
          Python.

          Thanks,
          Chris M.

          "Roman Suzi" <rnd@onego.ru > wrote in message
          news:mailman.55 .1130998328.187 01.python-list@python.org ...[color=blue]
          > On Thu, 3 Nov 2005, Chris McCoy wrote:
          >[color=green]
          >> Thank you! I've been banging my head against the wall!
          >>
          >> Chris M.[/color]
          >[color=green][color=darkred]
          >>> gridSystemId = [[None]*columns]*rows[/color]
          >>
          >> You've made gridSystemID a list of `rows` references to the SAME "inner"
          >> list, so the behavior you observe is the only possible one.
          >>
          >> If you want copies instead, ASK for copies...:
          >>
          >> gridSystemId = [ [None]*columns for x in xrange(rows) ][/color]
          >
          >
          > Interesting, could not pychecker recognize such situations in Python
          > code and give warnings?
          >
          >
          > Sincerely yours, Roman Suzi
          > --
          > rnd@onego.ru =\= My AI powered by GNU/Linux RedHat 7.3
          >[/color]



          ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
          http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
          ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

          Comment

          • Fredrik Lundh

            #6
            Re: Nested List Question

            "Newsfeeds" <chris.mccoy@sp irentcom.com> wrote:
            [color=blue]
            > Could anyone tell me why this code produces the output it does?[/color]



            has the full story.

            </F>



            Comment

            • Mike Meyer

              #7
              Re: Pychecker Re: Nested List Question

              Roman Suzi <rnd@onego.ru > writes:[color=blue]
              > On Thu, 3 Nov 2005, Chris McCoy wrote:[color=green][color=darkred]
              >>> gridSystemId = [[None]*columns]*rows[/color]
              >> You've made gridSystemID a list of `rows` references to the SAME "inner"
              >> list, so the behavior you observe is the only possible one.
              >> If you want copies instead, ASK for copies...:
              >> gridSystemId = [ [None]*columns for x in xrange(rows) ][/color]
              > Interesting, could not pychecker recognize such situations in Python
              > code and give warnings?[/color]

              Well, it could always just issue warnings everytime it saw a list
              multiplied by something. But that would get annoying in the cases
              where that idiom doesn't have problems - which is naturally most such
              usages. Your example included one such, which is why the translation
              wasn't to:

              gridSystemId = [[None for y in xrange(columns)] for x in xrange(rows)] WRONG

              [None] * columns doesn't have problems. Nor does any other immutable
              object. So to avoid spurious warnings, pychecker would have to know
              whether the objects in the list were immutable or not. It could guess
              that if the objects are builtin types, but not for other types.

              <mike
              --
              Mike Meyer <mwm@mired.or g> http://www.mired.org/home/mwm/
              Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

              Comment

              Working...