python said : "1, 2, 3, 6, 7, manbo !"

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

    python said : "1, 2, 3, 6, 7, manbo !"

    At the python2.3.2 prompt
    [color=blue][color=green][color=darkred]
    >>> a = range(8)
    >>> for b in a:[/color][/color][/color]
    if b == 4:
    a.remove(b)
    else:
    print b


    0
    1
    2
    3
    6
    7


    Why 5 does not appear ? (this was the source of a deep bug in a 4000+
    lines networked program...)

    RodrigoB

  • Rodrigo Benenson

    #2
    Re: python said : "1, 2, 3, 6, 7, manbo !"

    (corrected the identation)
    [color=blue]
    > At the python2.3.2 prompt
    >[color=green][color=darkred]
    > >>> a = range(8)
    > >>> for b in a:[/color][/color]
    > ...........if b == 4:
    > ............... .a.remove(b)
    > ...........else :
    > ............... .print b
    >
    >
    > 0
    > 1
    > 2
    > 3
    > 6
    > 7
    >
    >
    > Why 5 does not appear ? (this was the source of a deep bug in a 4000+
    > lines networked program...)
    >
    > RodrigoB
    >[/color]


    Comment

    • Ben Finney

      #3
      Re: python said : "1, 2, 3, 6, 7, manbo !"

      On Wed, 21 Jan 2004 17:53:19 -0300, - wrote:[color=blue]
      > Why 5 does not appear ? (this was the source of a deep bug in a 4000+
      > lines networked program...)[/color]

      Curious:
      [color=blue][color=green][color=darkred]
      >>> a = range(8)
      >>> for b in a:[/color][/color][/color]
      ... if ( b == 4 ):
      ... a.remove(b)
      ... print ( b, a )
      ...
      (0, [0, 1, 2, 3, 4, 5, 6, 7])
      (1, [0, 1, 2, 3, 4, 5, 6, 7])
      (2, [0, 1, 2, 3, 4, 5, 6, 7])
      (3, [0, 1, 2, 3, 4, 5, 6, 7])
      (4, [0, 1, 2, 3, 5, 6, 7])
      (6, [0, 1, 2, 3, 5, 6, 7])
      (7, [0, 1, 2, 3, 5, 6, 7])

      It seems that, having removed the current item in the list, the index of
      the "for" loop hasn't changed. The next iteration increments that
      index, even though the list itself has become shorter.

      --
      \ "You know I could rent you out as a decoy for duck hunters?" |
      `\ -- Groucho Marx |
      _o__) |
      Ben Finney <http://bignose.squidly .org/>

      Comment

      • Erik Max Francis

        #4
        Re: python said : &quot;1, 2, 3, 6, 7, manbo !&quot;

        - wrote:
        [color=blue]
        > Why 5 does not appear ? (this was the source of a deep bug in a 4000+
        > lines networked program...)[/color]

        You are iterating over a mutable sequence while you are mutating it.
        That is a big no-no.

        --
        __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
        / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
        \__/ Life is painting a picture, not doing a sum.
        -- Oliver Wendell Holmes, Jr.

        Comment

        • Ben Finney

          #5
          Re: python said : &quot;1, 2, 3, 6, 7, manbo !&quot;

          On Wed, 21 Jan 2004 19:03:44 -0800, Erik Max Francis wrote:[color=blue]
          > You are iterating over a mutable sequence while you are mutating it.
          > That is a big no-no.[/color]

          Got a pointer to somewhere that says so? Or at least describes the
          expected behaviour for iteration over mutable types?

          --
          \ "Love is the triumph of imagination over intelligence." -- |
          `\ Henry L. Mencken |
          _o__) |
          Ben Finney <http://bignose.squidly .org/>

          Comment

          • Erik Max Francis

            #6
            Re: python said : &quot;1, 2, 3, 6, 7, manbo !&quot;

            Ben Finney wrote:
            [color=blue]
            > Got a pointer to somewhere that says so? Or at least describes the
            > expected behaviour for iteration over mutable types?[/color]

            Tutorial, section 4.2:



            "It is not safe to modify the sequence being iterated over in the loop
            (this can only happen for mutable sequence types, such
            as lists). If you need to modify the list you are iterating over (for
            example, to duplicate selected items) you must iterate over
            a copy."

            This is actually not specific to Python; in C++'s Standard Library, for
            instance, there are complicated ramifications of modifying a container's
            contents while you're actively maintaining and using an iterator over
            it.

            --
            __ Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
            / \ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
            \__/ Isn't the best defense always a good attack?
            -- Ovid

            Comment

            • Francis Avila

              #7
              Re: python said : &quot;1, 2, 3, 6, 7, manbo !&quot;


              Erik Max Francis wrote in message <400F3D90.21760 87B@alcyone.com >...[color=blue]
              >- wrote:
              >[color=green]
              >> Why 5 does not appear ? (this was the source of a deep bug in a 4000+
              >> lines networked program...)[/color]
              >
              >You are iterating over a mutable sequence while you are mutating it.
              >That is a big no-no.
              >[/color]

              Try this instead:

              a = [i for i in range(8) if not i == 4]
              --
              Francis Avila

              Comment

              • Miki Tebeka

                #8
                Re: python said : &quot;1, 2, 3, 6, 7, manbo !&quot;

                Hello Rodrigo,[color=blue][color=green][color=darkred]
                > >>> a = range(8)
                > >>> for b in a:[/color][/color]
                > if b == 4:
                > a.remove(b)
                > else:
                > print b[/color]
                try:
                a = range(8)
                for b in list(a): # Iterate over a shallow copy
                if b == 4:
                a.remove(b)
                else:
                print b
                0
                1
                2
                3
                5
                6
                7

                HTH.
                Miki

                Comment

                • Edward C. Jones

                  #9
                  Re: python said : &quot;1, 2, 3, 6, 7, manbo !&quot;

                  Erik Max Francis wrote:[color=blue]
                  > - wrote:
                  >
                  >[color=green]
                  >>Why 5 does not appear ? (this was the source of a deep bug in a 4000+
                  >>lines networked program...)[/color]
                  >
                  >
                  > You are iterating over a mutable sequence while you are mutating it.
                  > That is a big no-no.[/color]

                  This can sometimes be done safely by iterating backwards:

                  n = len(seq)
                  for i in range(n-1, -1, -1):
                  ...

                  Comment

                  Working...