473,549 Members | 2,715 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Compare similar dictionary entries

sicarie
4,677 Recognized Expert Moderator Specialist
I'm querying two different systems by username/email and comparing the results, however I'm not incredibly familiar with python and don't know the best way to compare the dictionary entries.

The three quirky things are:

1) The results are returned in a dictionary (from both systems), whose values are a list of user attributes, and I'd like to compare either the username or the email fields in the list inside the dictionary (preferably email as it's required to be unique)

2) The format is 'clean' on one return - ie email@domain.co m but in list form from another, ie ['email2@domain2 .com'] (same with username) - not sure if this matters, it hasn't seemed to yet

3) there may be multiple email addresses. Only one is required to be unique, and one of the dictionaries may have multiple emails (though it is contained to a single source - say source 1's dictionary may have multiples, source 2's does not)

I've tried many things, what I'm looking at now is something like:
Expand|Select|Wrap|Line Numbers
  1. for key, value in data: #source2
  2.   for k, v in result: # source1 (possibly multiple emails)
  3.     for entry in value:
  4.       #this now looks wrong to me, I might not need the for above, but it still doesn't work without...
  5.       if entry.get("mail",[]) in v.get("mail",[]):
  6.         print "Match"
  7.         print value
  8.         print v
  9.         # eventually will join additional fields
  10.         # eventually will write to file
  11.       else:
  12.         #doesn't match, skip for now
  13.         pass
  14.  
Jun 11 '14 #1
4 1365
bvdet
2,851 Recognized Expert Moderator Specialist
Can you post a small representable sample of both data? If data (source 2) is a dictionary, you would need to iterate like this:
Expand|Select|Wrap|Line Numbers
  1. for key, value in data.items():
OR
Expand|Select|Wrap|Line Numbers
  1. for key in data:
  2.     value = data[key]
Jun 11 '14 #2
sicarie
4,677 Recognized Expert Moderator Specialist
Yep, I made some changes to it, but didn't want to keep editing my post on the fly. I can access the elements through:

Expand|Select|Wrap|Line Numbers
  1. for key, value in data: #data=list, value=dict(most of the time)
  2.     if isinstance(value,dict):
  3.         for k, v in result: # result=list, v=dict(most fo the time)
  4.             if isinstance(v,dict):
  5.                 if value.get("mail",[]) in v.get("mail",[]):
  6.                     print "Match"
  7.                     print value
  8.                     print v
  9.  
It's getting down to the value.get and v.get, but the if <item1> in <item2>: is not matching, It's a list of strings, but I can't figure out how to and how else I can compare.

Sample data from data:
Key (string) = User,Group (I disregard this except for iteration)
Value (dictionary) = {'mail': ['email@domain.c om'], 'type': ['code'], 'name': ['User Name']}

Sample data from result:
k (string) = User,group (again I generally disregard)
v (dict) = {'mail': ['email@domain.c om','email2@dom ain.com'], 'group': ['group'], 'type': ['code'], 'name': ['User Name']}

And when I iterate through either value or v I get that they are lists, but I can't seem to pull the individual values with what I'm coding. Not sure if that's relevant :)

Edit: I'm correlating two groups together, and I'm trying to find the people that match between the groups, as well as those that are only from source1 or only from source2. The source with multiple emails is the only thing I can guarantee will be unique, which is why I chose that to sort on. I'm definitely open to other types, but I wasn't sure if set differences would catch everything I'm looking for
Jun 11 '14 #3
sicarie
4,677 Recognized Expert Moderator Specialist
I got it, it was a list, so I assigned it to a variable and converted it to a string and the comparison worked

Thanks for the help bvdet!
Jun 11 '14 #4
bvdet
2,851 Recognized Expert Moderator Specialist
Glad you were able to work it out. Thanks for the feedback.
Jun 11 '14 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2863
by: Tim Daneliuk | last post by:
I am aware that dictionary order is not guaranteed. But I ran into something puzzling today. I filled a dictionary dynamically as a program ran. Up to a certain point, the key order was the order in which things were added. However, at some point, the dictionary appeared to reorder its contents in some non-deterministic way - in fact, it...
4
2507
by: brianobush | last post by:
# # My problem is that I want to create a # class, but the variables aren't known # all at once. So, I use a dictionary to # store the values in temporarily. # Then when I have a complete set, I want to # init a class from that dictionary. # However, I don't want to specify the # dictionary gets by hand # since it is error prone.
1
4498
by: Martin Widmer | last post by:
Hi Folks. When I iterate through my custom designed collection, I always get the error: "Unable to cast object of type 'System.Collections.DictionaryEntry' to type 'ContentObjects.ContentBlock'." The error occurs at the "For...Each" line if this method:
6
1678
by: Ilias Lazaridis | last post by:
remark: not sure if the term "dictionary" is correct here. I have the following situation: within a setup.cfg, settings are passed this way: settings=project_page=theProjectPage.com myVar=myValue those are accessible later like this:
11
14455
by: John Henry | last post by:
Hi list, I am sure there are many ways of doing comparision but I like to see what you would do if you have 2 dictionary sets (containing lots of data - like 20000 keys and each key contains a dozen or so of records) and you want to build a list of differences about these two sets. I like to end up with 3 lists: what's in A and not in B,...
1
1458
by: keleathi | last post by:
I'm pulling in data from two servers over two SSH tunnels. The tables on each of the servers have the same fields, 8 in total. I need a way to see if an entry that is in the first server is also on the second server. Out of 8 of the fields, 5 will have to match to be considered a match (but I need to pull all 8 in order to use them in the...
8
7967
by: Bob Altman | last post by:
Hi all, I'm trying to do something that should be really easy, but I can't think of an obvious way to do it. I have a dictionary whose value is a "value type" (as opposed to a reference type -- in this case, a Boolean): Dim dic As New Dictionary(Of Int32, Boolean) dic(123) = True I want to set all of the existing entries in the...
4
1550
by: John Townsend | last post by:
Joe had a good point! Let me describe what problem I'm trying to solve and the list can recommend some suggestions. I have two text files. Each file contains data like this: Test file 1234 4567 8975 I want to compare the numbers in each text file. The data set (i.e. the numbers) has a unique identifier: the "test" + the "file". The text...
2
3206
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I'm pulling records from or database by dates, and there are multiple records for each part number. The part numbers are being stored as strings in a List in a custom Employee class: List<stringParts = new List<string>(); If a part number is not already in the list, a new entry is created for an employee that gets credited this part...
2
4809
by: plotdevice | last post by:
I have a text file that has a frequently-changing list of filenames in it. exampleList.txt: fee.doc fye.doc foe.doc fum.doc ftangftang_ole_biscuitbarrel.doc
0
7446
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7715
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. ...
0
7956
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...
0
6040
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5368
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...
0
5087
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...
0
3498
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...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1935
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

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.