???? i can`t understand it

Discussion in 'Python' started by Enrique, Aug 8, 2003.

  1. Enrique

    Enrique Guest

    >>> a=[1,2,3,4,5]
    >>> for b in a:

    .... a.remove(b)
    ....
    >>> a

    [2, 4]
    >>>
     
    Enrique, Aug 8, 2003
    #1
    1. Advertising

  2. Enrique

    David C. Fox Guest

    Enrique wrote:
    > >>> a=[1,2,3,4,5]
    > >>> for b in a:

    > ... a.remove(b)
    > ...
    > >>> a

    > [2, 4]
    > >>>


    As others have noted, modifying a sequence can have strange effects on
    iterators.

    Presumably, the example above is simplified (since if you really wanted
    to remove all elements in a, you would just say a = []). If you want to
    selectively remove elements in a, you should have a look at the built-in
    function filter. For example, to remove elements < 0:

    a = [1,5,7, -4, 2, -10]
    a = filter(lambda x: x >= 0, a)

    David
     
    David C. Fox, Aug 8, 2003
    #2
    1. Advertising

  3. Enrique wrote:

    >>>> a=[1,2,3,4,5]
    >>>> for b in a:

    > ... a.remove(b)
    > ...
    >>>> a

    > [2, 4]
    >>>>

    Very interesting result but reasonable. If the underlying interpreter uses
    a reference or pointer for the job like this:

    # psudo codes for python
    for(ptr = a.first(); ptr.is_valid(); ++ptr)
    {
    update('b', value(ptr))
    call_method('a', 'remove', get_alue('b'))
    }

    ptr may be just an index. So at the first iteration, the first element, '1',
    is removed from a. The next time ptr=1, but a has been changed to [2,3,4,5],
    so '3' is removed, and so on,


    Smille
     
    Smille Purusa, Aug 12, 2003
    #3
  4. Enrique

    Robin Munn Guest

    Smille Purusa <> wrote:
    > Enrique wrote:
    >
    >>>>> a=[1,2,3,4,5]
    >>>>> for b in a:

    >> ... a.remove(b)
    >> ...
    >>>>> a

    >> [2, 4]
    >>>>>

    > Very interesting result but reasonable. If the underlying interpreter uses
    > a reference or pointer for the job like this:
    >
    > # psudo codes for python
    > for(ptr = a.first(); ptr.is_valid(); ++ptr)
    > {
    > update('b', value(ptr))
    > call_method('a', 'remove', get_alue('b'))
    > }
    >
    > ptr may be just an index. So at the first iteration, the first element, '1',
    > is removed from a. The next time ptr=1, but a has been changed to [2,3,4,5],
    > so '3' is removed, and so on,


    This is exactly correct. From the Python reference manual, describing
    for loops:

    Warning: There is a subtlety when the sequence is being modified by
    the loop (this can only occur for mutable sequences, i.e. lists). An
    internal counter is used to keep track of which item is used next,
    and this is incremented on each iteration. When this counter has
    reached the length of the sequence the loop terminates. This means
    that if the suite deletes the current (or a previous) item from the
    sequence, the next item will be skipped (since it gets the index of
    the current item which has already been treated). Likewise, if the
    suite inserts an item in the sequence before the current item, the
    current item will be treated again the next time through the loop.
    This can lead to nasty bugs that can be avoided by making a
    temporary copy using a slice of the whole sequence, e.g.,

    for x in a[:]:
    if x < 0: a.remove(x)

    You can read the whole thing at:
    http://www.python.org/doc/current/ref/for.html

    --
    Robin Munn <> | http://www.rmunn.com/ | PGP key 0x6AFB6838
    -----------------------------+-----------------------+----------------------
    "Remember, when it comes to commercial TV, the program is not the product.
    YOU are the product, and the advertiser is the customer." - Mark W. Schumann
     
    Robin Munn, Aug 12, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil
    Replies:
    11
    Views:
    8,582
    vipinlal
    Mar 11, 2010
  2. =?Utf-8?B?S2VubmV0aCBQ?=

    An error message I can't understand

    =?Utf-8?B?S2VubmV0aCBQ?=, Mar 12, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    2,855
    Jeff Dillon
    Aug 6, 2008
  3. JV
    Replies:
    1
    Views:
    348
  4. Learner
    Replies:
    1
    Views:
    435
    Kevin Spencer
    Jan 5, 2006
  5. Paul F. Johnson

    I can't understand this problem

    Paul F. Johnson, Oct 12, 2004, in forum: HTML
    Replies:
    2
    Views:
    347
Loading...

Share This Page