473,503 Members | 1,864 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.com 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 1362
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.com'], 'type': ['code'], 'name': ['User Name']}

Sample data from result:
k (string) = User,group (again I generally disregard)
v (dict) = {'mail': ['email@domain.com','email2@domain.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
2757
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...
4
2502
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...
1
4492
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...
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...
11
14448
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...
1
1454
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...
8
7956
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 --...
4
1547
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...
2
3202
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: ...
2
4806
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
7093
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...
1
7006
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7467
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5592
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,...
0
4685
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...
0
3175
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...
0
3166
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1519
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 ...
0
397
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...

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.