473,399 Members | 2,278 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Compare similar dictionary entries

sicarie
4,677 Expert Mod 4TB
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]

4 1359
bvdet
2,851 Expert Mod 2GB
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 Expert Mod 4TB
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 Expert Mod 4TB
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 Expert Mod 2GB
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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
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
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...

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.