Compare similar dictionary entries

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    Compare similar dictionary entries

    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:
    Code:
    for key, value in data: #source2
      for k, v in result: # source1 (possibly multiple emails)
        for entry in value:
          #this now looks wrong to me, I might not need the for above, but it still doesn't work without...
          if entry.get("mail",[]) in v.get("mail",[]):
            print "Match"
            print value
            print v
            # eventually will join additional fields
            # eventually will write to file
          else:
            #doesn't match, skip for now
            pass
    Last edited by sicarie; Jun 11 '14, 05:26 PM. Reason: code fix - that's what happens when I type and don't copy/paste
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Can you post a small representable sample of both data? If data (source 2) is a dictionary, you would need to iterate like this:
    Code:
    for key, value in data.items():
    OR
    Code:
    for key in data:
        value = data[key]

    Comment

    • sicarie
      Recognized Expert Specialist
      • Nov 2006
      • 4677

      #3
      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:

      Code:
      for key, value in data: #data=list, value=dict(most of the time)
          if isinstance(value,dict):
              for k, v in result: # result=list, v=dict(most fo the time)
                  if isinstance(v,dict):
                      if value.get("mail",[]) in v.get("mail",[]):
                          print "Match"
                          print value
                          print v
      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
      Last edited by sicarie; Jun 11 '14, 07:18 PM. Reason: Edit: Searching

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        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!

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          Glad you were able to work it out. Thanks for the feedback.

          Comment

          Working...