#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    40
    Rep Power
    11

    Comparing two lists


    I am stuck and could use a nudge in the right direction.

    I have two lists, badguys and goodguys.

    what I want to do is check for each value of goodguys, is present in any of the spot in badguys, and if this is true, then to remove the common value from the list badguys.

    :edit
    I guess I shoud add the lists contain strings (ip addresses).
    :edit

    I have been playing around with while loops and for loops, but I cant figure it out. any help , much appreciated, as always.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    hmm, if i am understanding you right, i think something like this would work:

    Code:
    >>> badGuys = ['192.392.553.1', '134.133.44.4']
    >>> goodGuys = ['443.113.445.1', '134.133.44.4']
    >>> for i in goodGuys:
    ...     try:
    ...             location = badGuys.index(i)
    ...             badGuys.remove(i)
    ...     except ValueError:
    ...             pass
    ...
    >>> print badGuys
    ['192.392.553.1']
    what this does is test to see if badGuys has any value of that goodguys has and if so it deletes the value from badGuys. Hope this helps, if im wrong let me know and i will be glad to help out more
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    40
    Rep Power
    11
    Yes!!!! Thank you!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    This is much simpler (well, IMO ) with list-comprehension:
    Code:
    >>> badGuys = ['192.392.553.1', '134.133.44.4']
    >>> goodGuys = ['443.113.445.1', '134.133.44.4']
    >>> badGuys = [i for i in badGuys if i not in goodGuys]
    >>> badGuys
    ['192.392.553.1']
    Regards, mawe
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by mawe
    This is much simpler (well, IMO ) with list-comprehension:
    Code:
    >>> badGuys = ['192.392.553.1', '134.133.44.4']
    >>> goodGuys = ['443.113.445.1', '134.133.44.4']
    >>> badGuys = [i for i in badGuys if i not in goodGuys]
    >>> badGuys
    ['192.392.553.1']
    Regards, mawe
    If the lists are large this can be made more efficient by creating a set from the goodGuys list:

    Code:
    >>> badGuys = ['192.392.553.1', '134.133.44.4']
    >>> goodGuys = set(['443.113.445.1', '134.133.44.4'])
    >>> badGuys = [i for i in badGuys if i not in goodGuys]
    >>> badGuys
    ['192.392.553.1']
    The 'in' operator for lists is proportional to the length of the list while for sets it is constant time, so the above code could be thousands of time faster for very large lists.

    This is for Python 2.4, which has 'set' as a built-in datatype.
    For Python 2.3 use the sets module. For earlier versions of Python use a dictionary instead.

    If the order of the resulting list is not important then you could put both lists into sets and use set algebra, e.g. result = badGuys - goodGuys.

    Dave - The Developers' Coach
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2005
    Location
    Saint Kiev Russia
    Posts
    13
    Rep Power
    0
    Code:
    badGuys = ['192.392.553.1', '134.133.44.4','127.0.0.1']
    goodGuys = ['443.113.445.1', '134.133.44.4','127.0.0.1']
    badGuys=filter(lambda s: s not in goodGuys,badGuys)
    print badGuys
    
    ['192.392.553.1']
    i think this is a good python style)

IMN logo majestic logo threadwatch logo seochat tools logo