Function to remove elements from a list not working (corrected)

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

    #1

    Function to remove elements from a list not working (corrected)

    Hi,
    I am trying to modify a list of pairs (l4) by removing those
    pairs which are not present in a third list called pairList.
    The following is a simplified part of the routine i have written. However
    it does not give the correct output. Please help!
    Its possible i have made a trivial mistke since i am a newbie.

    def getl5():
    l5 = []
    pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]]
    l4 = [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]]
    for pair in l4:
    if pair not in pairList:
    l4.remove(pair)
    print "l4 is",l4

    The output given is:
    l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]]
  • wittempj@hotmail.com

    #2
    Re: Function to remove elements from a list not working (corrected)


    Girish Sahani wrote:[color=blue]
    > Hi,
    > I am trying to modify a list of pairs (l4) by removing those
    > pairs which are not present in a third list called pairList.
    > The following is a simplified part of the routine i have written. However
    > it does not give the correct output. Please help!
    > Its possible i have made a trivial mistke since i am a newbie.
    >
    > def getl5():
    > l5 = []
    > pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]]
    > l4 = [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]]
    > for pair in l4:
    > if pair not in pairList:
    > l4.remove(pair)
    > print "l4 is",l4
    >
    > The output given is:
    > l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]][/color]

    It is better to iterate over a copy, e.g. like this:

    pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]]
    l4 =
    [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]]
    for pair in l4[:]:
    if pair not in pairList:
    l4.remove(pair)
    print "l4 is",l4

    Comment

    • Girish Sahani

      #3
      Re: Function to remove elements from a list not working (corrected)

      Thank you Mark....this works too...
      Btw going slightly off-topic, when i try to run a code like below with
      around 50 elements (pairs) in l4,python just hangs. Any ideas why this is
      happening...the data is not that large :(([color=blue]
      >
      > Girish Sahani wrote:[color=green]
      >> Hi,
      >> I am trying to modify a list of pairs (l4) by removing those
      >> pairs which are not present in a third list called pairList.
      >> The following is a simplified part of the routine i have written.
      >> However
      >> it does not give the correct output. Please help!
      >> Its possible i have made a trivial mistke since i am a newbie.
      >>
      >> def getl5():
      >> l5 = []
      >> pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]]
      >> l4 =
      >> [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]]
      >> for pair in l4:
      >> if pair not in pairList:
      >> l4.remove(pair)
      >> print "l4 is",l4
      >>
      >> The output given is:
      >> l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]][/color]
      >
      > It is better to iterate over a copy, e.g. like this:
      >
      > pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]]
      > l4 =
      > [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]]
      > for pair in l4[:]:
      > if pair not in pairList:
      > l4.remove(pair)
      > print "l4 is",l4
      >
      > --
      > http://mail.python.org/mailman/listinfo/python-list
      >[/color]

      Comment

      • Boris Borcic

        #4
        Re: Function to remove elements from a list not working (corrected)

        Girish Sahani wrote:[color=blue]
        > Hi,
        > I am trying to modify a list of pairs (l4) by removing those
        > pairs which are not present in a third list called pairList.
        > The following is a simplified part of the routine i have written. However
        > it does not give the correct output. Please help!
        > Its possible i have made a trivial mistke since i am a newbie.
        >
        > def getl5():
        > l5 = []
        > pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]]
        > l4 = [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]]
        > for pair in l4:
        > if pair not in pairList:
        > l4.remove(pair)
        > print "l4 is",l4
        >
        > The output given is:
        > l4 is [[4, 7], [4, 12], [9, 7], [9, 12], [11, 7]][/color]

        use sets

        def gets5() :
        pairSet = set([(1,2),(3,4),(3, 5),(3,6),(9,7), (8,9),(8,7),(7, 9),(11,10)])
        s4= set([(4,2),(4,7),(4, 10),(4,12),(9,2 ),(9,7),(9,10), (9,12),(11,2),( 11,7)])
        s4 &= pairSet
        print "s4 is",s4

        the output is

        s4 is set([(9, 7)])

        Comment

        • Paul McGuire

          #5
          Re: Function to remove elements from a list not working (corrected)

          "Girish Sahani" <girish@cse.iit b.ac.in> wrote in message
          news:mailman.69 16.1150100741.2 7775.python-list@python.org ...[color=blue]
          > Hi,
          > I am trying to modify a list of pairs (l4) by removing those
          > pairs which are not present in a third list called pairList.
          > The following is a simplified part of the routine i have written. However
          > it does not give the correct output. Please help!
          > Its possible i have made a trivial mistke since i am a newbie.
          >[/color]

          You've fallen victim to one of the Classic Blunders! The First is "Never
          start a land war in Asia!", but the second, only slightly lesser known is
          "Never modify a list that you are iterating over!"

          -- Paul


          Comment

          • Fredrik Lundh

            #6
            Re: Function to remove elements from a list not working (corrected)

            Girish Sahani wrote:
            [color=blue]
            > Btw going slightly off-topic, when i try to run a code like below with
            > around 50 elements (pairs) in l4,python just hangs. Any ideas why this is
            > happening...the data is not that large :(([/color]

            building a filtered new list by repeatedly removing stuff from a copy
            of the original list isn't exactly the fastest way to do things, but
            there's no way the following code will "hang" with 50 items instead of
            10, unless your computer is ludicrously slow (it takes about 0.000113
            seconds on my machine).

            maybe you meant to write 50k items ? (11 seconds on my machine)
            [color=blue][color=green]
            >> pairList = [[1,2],[3,4],[3,5],[3,6],[9,7],[8,9],[8,7],[7,9],[11,10]]
            >> l4 =
            >> [[4,2],[4,7],[4,10],[4,12],[9,2],[9,7],[9,10],[9,12],[11,2],[11,7]]
            >> for pair in l4[:]:
            >> if pair not in pairList:
            >> l4.remove(pair)
            >> print "l4 is",l4[/color][/color]

            </F>

            Comment

            Working...