Compare two dictionaries and print out missing or no matches

vmx1987

I'm new to python and would really like to get some direction here.

I have two almost identical dictionaries - First_Dict and Second_Dict

First_Dict = {"Texas": ["San Antonio", "Austin", "Houston", "Dallas"], 
         "California": ["San Diego", "Los Angeles", "San Francisco"],
        "Florida": ["Miami", "Orlando", "Jacksonville", "Naples"], 
         "Arizona": ["Phoenix", "Tucson"]}


Second_Dict = {"Texas": ["San Antonio, Austin, Houston"],
           "California": ["San Diego, Los Angeles, San Francisco"],
           "Florida": ["Miami", "Orlando", "Jacksonville"], "Illinois": 
          ["Chicago", "Naperville"]}

Goal: I need to compare them in the following flow:

Compare keys
    if key match
        compare values
            if all values match
                break
            else:
                print the key and the corresponding missing value/s.
                    "Missing value/s on key "Florida" in the Second_Dict"
                        "Naples"

    if keys NOT match or missing
        print the unmatching/missing key and corresponding value/s.
            "Missing key and value/s on First_Dict"
                Illinois
                    Chicago
                    Naperville

            "Missing key and value/s on Second_Dict"
                Arizona
                    Phoenix
                    Tucson

My code isn't much so far :) Sorry, still learning.

for key, value in First_Dict.items() and Second_Dict.items():
    if key in First_Dict.keys() == Second_Dict.keys():
       for value in First_Dict.value() and Second_Dict.value :
          if value in First_Dict.value() == Second_Dict.value():
              break
          else:
              print(value)
Iulian

I imagine that you want to know differences not only for the first dictionary from second, but also vice versa. For me a good way is to separate the control in the following steps:

  1. Find common keys to both dictionary.
  2. Using the common keys compute values differences in both dictionaries.
  3. Indicate the missing keys with relative values.

Possible code:

#Step 1
#Determination of common keys 
first_keys = first_Dict.keys() #retrieve keys of the dictionary
second_keys = second_Dict.keys()
common_keys = [key for key in first_keys if key in second_keys]

#Step 2
#so now for common keys we look for differences in value and printing them
for common in common_keys:
  townsA = first_Dict[common]
  townsB = second_Dict[common]

  #with the first statement determine the cities that are in the second
  #dictionary but not in first one.
  #with the second the opposite 
  missingOnFirst = [town for town in townsB if town not in townsA]
  missingOnSecond = [town for town in townsA if town not in townsB]

  if missingOnFirst:
    print("Missing on {0} in first dictionary: \n\t{1}".format(common,"\n\t".join(missingOnFirst)))
  if missingOnSecond:
    print("Missing on {0} in second dictionary: \n\t{1}".format(common,"\n\t".join(missingOnSecond)))

#Step 3
#printing the missing keys:
#on First dictionary
print("\n")
print("Missing key and value/s on first dictionary")
for key in second_keys:
  if key not in common_keys:
    print("{0}:\n\t{1}".format(key,"\n\t".join(second_Dict[key])))
#on Second dictionary
print("Missing key and value/s on second dictionary")
for key in first_keys:
  if key not in common_keys:
    print("{0}:\n\t{1}".format(key,"\n\t".join(first_Dict[key])))

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Compare two dictionaries for matches

Compare Two Dictionaries and Print Difference

Compare two int arrays and print the missing numbers

compare two results and print the missing part if exist

Compare two lists and print what is missing

Compare two dictionaries and add missing keys.Values not important at this time

Print available and show missing items from two dictionaries

Compare two files and print matches in the first file adding extra column

Compare two lists of dictionaries

Compare Two Ordered Dictionaries

Compare two List of Dictionaries

compare two dictionaries in python

Checking for exact matches in two dictionaries

Python : Compare two csv files and print out differences

Compare numbers in two text documents and print out if the difference is too big

Compare two String[] arrays and print out the strings which differ

Compare two list with dicts and print out value that is not in the list using Python?

Compare two lists and print out when a change happens

Perl (compare two file) print out differences on main file only

Compare two arrays and print out Index of row in python

How to compare two pairs of columns in two different files and print matches merged horizontally (preference for BASH or AWK)?

Dictionary comprehension to compare two dictionaries

How to compare two dictionaries and update

Compare two list of dictionaries in python

how to compare two dictionaries in python

How to compare between two dictionaries?

compare two dictionaries key by key

Compare two strings and replace matches

bash - compare two columns of one file with one column of second file and print matches