简体   繁体   中英

Python read two dictionaries compare values and create third dictionary

I have two dictionary files that I want to compare. I need to know if a value from one dictionary occurs in the other dictionary. The keys are not the same so I can't go based on keys.

Goals:

  1. Does same value exist in both dictionaries?
  2. Print the values that do not occur in the other dictionary.
  3. Create a new dictionary with the Keys, values from both the dictionary that match and print dictionary to screen.

I'm able to search both dictionaries and find 69 different entries.

My issues are:

  1. I suspect there is a much simpler way of doing this and
  2. I suspect a better approach overall.
  3. I'm not creating the third dictionary with the values/keys from the two when matched.

Current Code:

#!/usr/bin/python

# Open Files and Build dictionaries.

ackg2shipping = open('AirCheckG2_ShippingLog.txt', 'r', encoding='ascii')
dAckg2 = {}
for line in ackg2shipping:
    if not line.strip():
        continue
    row = line.split(',')
    # creating unique key as one does not exist in file:
    mfgDate = row[2]
    serialNumber = row[5]
    dAckg2[mfgDate] = serialNumber

macAddressLog = open('MacAddress.dat', 'r', encoding='ascii')
dMacaddress = {}
for line in macAddressLog:
    if not line.strip():
        continue
    row = line.replace(", ", ",").split(',')
    macAddress = row[0]
    serialNum2 = row[1]
    if serialNum2.find("_2") != -1:
        continue
    if serialNum2.find("HM") != -1:
        continue
    if serialNum2.find("-") != -1:
        continue
    if serialNum2.find("Min") != -1:
        continue
    if serialNum2.find("Max") != -1:
        continue
    dMacaddress[macAddress] = serialNum2

for ackValue, macValue in zip(dAckg2.items(), dMacaddress.items()):
        if ackValue == macValue:
            print('Ok', ackValue, macValue)
        else:
            print('Not', ackValue, macValue)


match = 0
nomatch = 0

for ackKey, ackValue in dAckg2.items():
    for macKey, macValue in dMacaddress.items():
        if ackValue == macValue:
            # print("Debug: ", macValue, ackValue, "Match")
            match += 1
        else:
            # print("no match")
            # print("Debug: ", macValue, ackValue)
            nomatch += 1

print("Matched: ", match)
print("Not Matched:", nomatch)

match = 0
nomatch = 0
for macKey, macValue in dMacaddress.items():
    for ackKey, ackValue in dAckg2.items():
        if macValue == ackValue:
            # print("Debug: ", macValue, ackValue, "Match")
            match += 1
        else:
            # print("no match")
            # print("Debug: ", macValue, ackValue)
            nomatch += 1

print("Matched: ", match)
print("Not Matched:", nomatch)
missingSerials = (len(dAckg2) - match)
# noinspection PyPep8
print(missingSerials)

If you're only concerned about the values, the dict.values() method is probably what you want.

d1_values = set(d1.values())
d2_values = set(d2.values())
in_both = d1_values & d2_values
not_in_both = d1_values ^ d2_values

I'm not sure exactly what your third requirement entails. Is it something like this?

new_dict = {}
for k, v in d1.items():
    if v in in_both:
       new_dict.update((k, v))
for k, v in d2.items():
    if v in in_both:
        new_dict.update((k, v))
print(new_dict)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM