473,803 Members | 2,279 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Delete common entries between two dictionaries

I have received such good help on this message board. I wonder if I might
not get a little more help from you on this.

I am at the point where I have two dictionaries, with information of a
domain and a frequency of that domain.

Now that I have the two, I want to delete each entry from one that the two
have in common, leaving only those that are unique to the dictionary?

Say I have a dictionary called domains_black
and another domains_white.. .

Thanks for the help.

Jul 18 '05 #1
11 2518
On Mon, 24 Nov 2003 13:24:17 -0800, Amy G wrote:
I have received such good help on this message board. I wonder if I
might not get a little more help from you on this.

I am at the point where I have two dictionaries, with information of
a domain and a frequency of that domain.

Now that I have the two, I want to delete each entry from one that
the two have in common, leaving only those that are unique to the
dictionary?
This would be great for sets, if a set adequately models your data.
(with two sets, this would simply be (s1-(s1&s2)))
Say I have a dictionary called domains_black and another
domains_white.. .


Did you want to define equality by key or by (key, value) pair?

for key in domains_white.k eys() :
if key in domains_black: del domains_black[key]

for key in domains_white.k eys() :
if key in domains_black and domains_white[key] == domains_black[key] :
del domains_black[key]

-D

--
He who scorns instruction will pay for it,
but he who respects a command is rewarded.
Proverbs 13:13

www: http://dman13.dyndns.org/~dman/ jabber: dm**@dman13.dyn dns.org
Jul 18 '05 #2
"Amy G" <am*******@cox. net> writes:
Now that I have the two, I want to delete each entry from one that the two
have in common, leaving only those that are unique to the dictionary?

Say I have a dictionary called domains_black
and another domains_white.. .


for k in domains_white() :
if k in domains_black:
del domains_black[k]
Jul 18 '05 #3
In article <97************ @dman13.dyndns. org>,
Derrick 'dman' Hudson <dm**@dman13.dy ndns.org> wrote:
This would be great for sets, if a set adequately models your data.
(with two sets, this would simply be (s1-(s1&s2)))


You mean s1 - s2, no need for that extra &.

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #4
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), ('buildingonlin e.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.windso ngnews.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), ('buildingonlin e.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.windso ngnews.com', 88), ('yahoo.ca', 435),
('yahoo.co.uk', 303), ('yahoo.com.hk' , 156), ('yahoo.fr', 266)]

ie. minus the entries in domains_white.

Thanks again guys.

AMY
"Derrick 'dman' Hudson" <dm**@dman13.dy ndns.org> wrote in message
news:97******** ****@dman13.dyn dns.org...
On Mon, 24 Nov 2003 13:24:17 -0800, Amy G wrote:
I have received such good help on this message board. I wonder if I
might not get a little more help from you on this.

I am at the point where I have two dictionaries, with information of
a domain and a frequency of that domain.

Now that I have the two, I want to delete each entry from one that
the two have in common, leaving only those that are unique to the
dictionary?


This would be great for sets, if a set adequately models your data.
(with two sets, this would simply be (s1-(s1&s2)))
Say I have a dictionary called domains_black and another
domains_white.. .


Did you want to define equality by key or by (key, value) pair?

for key in domains_white.k eys() :
if key in domains_black: del domains_black[key]

for key in domains_white.k eys() :
if key in domains_black and domains_white[key] == domains_black[key] :
del domains_black[key]

-D

--
He who scorns instruction will pay for it,
but he who respects a command is rewarded.
Proverbs 13:13

www: http://dman13.dyndns.org/~dman/ jabber:

dm**@dman13.dyn dns.org
Jul 18 '05 #5
In article <WGvwb.7230$9O5 .2236@fed1read0 6>,
"Amy G" <am*******@cox. net> wrote:
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


domains_black = [x for x in domains_black if x not in domains_white]

If domains_white is a long list, this will be inefficient due to the
linear search to test whether each x belongs to it. In that case, you
might be better off using a set:

mask = Set(domains_whi te)
domains_black = [x for x in domains_black if x not in mask]

Also, this creates a new list. If you instead want to change the same
list in-place, you could replace "domains_bl ack =" with
"domains_bl ack[:] =".

--
David Eppstein http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science
Jul 18 '05 #6
Don't know what I could have done wrong, but it just returned the origianl
list, unchanged.
"Amy G" <am*******@cox. net> wrote in message
news:WGvwb.7230 $9O5.2236@fed1r ead06...
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), ('buildingonlin e.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.windso ngnews.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), ('buildingonlin e.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.windso ngnews.com', 88), ('yahoo.ca', 435), ('yahoo.co.uk', 303), ('yahoo.com.hk' , 156), ('yahoo.fr', 266)]

ie. minus the entries in domains_white.

Thanks again guys.

AMY
"Derrick 'dman' Hudson" <dm**@dman13.dy ndns.org> wrote in message
news:97******** ****@dman13.dyn dns.org...
On Mon, 24 Nov 2003 13:24:17 -0800, Amy G wrote:
I have received such good help on this message board. I wonder if I
might not get a little more help from you on this.

I am at the point where I have two dictionaries, with information of
a domain and a frequency of that domain.

Now that I have the two, I want to delete each entry from one that
the two have in common, leaving only those that are unique to the
dictionary?


This would be great for sets, if a set adequately models your data.
(with two sets, this would simply be (s1-(s1&s2)))
Say I have a dictionary called domains_black and another
domains_white.. .


Did you want to define equality by key or by (key, value) pair?

for key in domains_white.k eys() :
if key in domains_black: del domains_black[key]

for key in domains_white.k eys() :
if key in domains_black and domains_white[key] == domains_black[key] : del domains_black[key]

-D

--
He who scorns instruction will pay for it,
but he who respects a command is rewarded.
Proverbs 13:13

www: http://dman13.dyndns.org/~dman/ jabber:

dm**@dman13.dyn dns.org

Jul 18 '05 #7
In article <7x************ @ruckus.brouhah a.com>,
Paul Rubin <http://ph****@NOSPAM.i nvalid> wrote:
"Amy G" <am*******@cox. net> writes:

Now that I have the two, I want to delete each entry from one that the two
have in common, leaving only those that are unique to the dictionary?

Say I have a dictionary called domains_black
and another domains_white.. .


for k in domains_white() :
if k in domains_black:
del domains_black[k]


Didja try that before posting?.... (I see at least two errors.)
--
Aahz (aa**@pythoncra ft.com) <*> http://www.pythoncraft.com/

Weinberg's Second Law: If builders built buildings the way programmers wrote
programs, then the first woodpecker that came along would destroy civilization.
Jul 18 '05 #8
aa**@pythoncraf t.com (Aahz) writes:
for k in domains_white() :
if k in domains_black:
del domains_black[k]


Didja try that before posting?.... (I see at least two errors.)


Oops, editing error (I removed 'keys' which is no longer needed, but
forgot to remove the parentheses). No I didn't try it first.
What's the second error?
Jul 18 '05 #9
* Amy G <am*******@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), ('buildingonlin e.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.windso ngnews.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), ('buildingonlin e.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.windso ngnews.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',11 8), .... ('buildingonlin e.com',130),('f oo.bar',100)] white = [('yahoo.com',11 ),('foo.bar',10 0)]
[x for x in black if x not in white] [('yahoo.com', 118), ('buildingonlin e.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]] [('buildingonlin e.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] [('buildingonlin e.com', 130)]


HTH-

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

Jul 18 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2754
by: Alistair | last post by:
IIS, Access 2000 I have a page that tabulates data from a DB, this data is items for sale, private ads. is there a way that at the same time it can automatically delete entries that are outside of a specific date..ie. more than a week old?? or should I run two queries seperately..one page which goes through the DB
8
13363
by: Brian L. Troutwine | last post by:
I've got a problem that I can't seem to get my head around and hoped somebody might help me out a bit: I've got a dictionary, A, that is arbitarily large and may contains ints, None and more dictionaries which themselves may contain ints, None and more dictionaries. Each of the sub-dictionaries is also arbitrarily large. When pretty printing A, in the context I'm using A for, it's rather helpful to remove all key:value pairs where value...
48
4442
by: Paul Melis | last post by:
Hello, I've always been using the has_key() method to test if a dictionary contains a certain key. Recently I tried the same using 'in', e.g. d = { ... } if k in d: ... and found that it seems to perform a lot better when lots of key-tests
29
4258
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to a temp array and delete afterwards if necessary but since I'm actually working in a nested situation this could get a little messy. I guess I could set there values to null and remove them afterwards? Thanks, Jon
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10542
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10309
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7600
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6840
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5496
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4274
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3795
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2968
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.