By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,101 Members | 1,335 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,101 IT Pros & Developers. It's quick & easy.

Compare similar dictionary entries

sicarie
Expert Mod 2.5K+
P: 4,677
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

✓ answered by bvdet

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]

Share this Question
Share on Google+
4 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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
Expert Mod 2.5K+
P: 4,677
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
Expert Mod 2.5K+
P: 4,677
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
Expert Mod 2.5K+
P: 2,851
Glad you were able to work it out. Thanks for the feedback.
Jun 11 '14 #5

Post your reply

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