Delete common entries between two lists (was dictionaries)

John Hazen invalid at hazen.net
Tue Nov 25 00:22:40 CET 2003


* Amy G <amy-g-art at cox.net> [2003-11-24 14:40]:
> How do I do this same thing but with lists???
> 
> I apparently have two lists... not dictionaries.
> 
> This is what it prints if I add
> print domains_black
> 
> [('YAHOO.COM', 118), ('buildingonline.com', 130), ('canada.com', 95),
> ('china.com', 104), ('earthlink.com', 118), ('earthlink.net', 122),
> ('email.com', 286), ('excite.com', 200), ('hongkong.com', 110), ('juno.com',
> 233), ('lycos.com', 95), ('mail.com', 399), ('minedu.fi', 134), ('msn.com',
> 764), ('shaw.ca', 259), ('stderr.windsongnews.com', 88), ('yahoo.ca', 435),
> ('yahoo.co.uk', 303), ('yahoo.com.hk', 156), ('yahoo.fr', 266)]
> 
> This is domains_white
> 
> [('aol.com', 17), ('awci.org', 6), ('cox.net', 12), ('hotmail.com', 6),
> ('yahoo.com', 11)]
> 
> I want to be left with domains_black =
> 
> [('YAHOO.COM', 118), ('buildingonline.com', 130), ('canada.com', 95),
> ('china.com', 104), ('earthlink.com', 118), ('earthlink.net', 122),
> ('email.com', 286), ('excite.com', 200), ('hongkong.com', 110), ('juno.com',
> 233), ('lycos.com', 95), ('mail.com', 399), ('minedu.fi', 134), ('msn.com',
> 764), ('shaw.ca', 259), ('stderr.windsongnews.com', 88), ('yahoo.ca', 435),
> ('yahoo.co.uk', 303), ('yahoo.com.hk', 156), ('yahoo.fr', 266)]
> 
> ie. minus the entries in domains_white.

Well, it's hard to tell exactly what you want, given that none of the
domains in the whitelist are in the original blacklist (and that you
didn't answer Derrick's question about whether you want to consider
entries equal if just the domain is the same, or do you require the
domain and the count to be the same).

(Also, I would recommend you normalize all of the domains to lowercase,
since case information is not significant to DNS.)

Anyway, I would solve the question you're asking with list
comprehensions:

>>> black = [('yahoo.com',118),
...          ('buildingonline.com',130),('foo.bar',100)]
>>> white = [('yahoo.com',11),('foo.bar',100)]
>>> [x for x in black if x not in white]
[('yahoo.com', 118), ('buildingonline.com', 130)]
>>> # note the version above only removes entries that have both 
... # domain and count equal.
... 
>>> [x for x in black if x[0] not in [y[0] for y in white]]
[('buildingonline.com', 130)]
>>> # I think the above is what you want.  I think it'll be
... # more readable with an intermediate assignment:
... 
>>> w = [y[0] for y in white]
>>> [x for x in black if x[0] not in w]
[('buildingonline.com', 130)]
>>>

HTH-

John
-- 
<my first name>@<my domain (see (invalid) from address)>





More information about the Python-list mailing list