Python read two dictionaries compare values and create third dictionary

邮差的信 提交于 2019-12-12 04:32:23

问题


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)

回答1:


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)


来源:https://stackoverflow.com/questions/40468546/python-read-two-dictionaries-compare-values-and-create-third-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!