0
我有两个字典文件,我想比较。我需要知道一个字典中的值是否出现在另一个字典中。钥匙是不一样的,所以我不能去基于钥匙。Python阅读两个字典比较值,并创建第三个字典
目标:
- 是否相同的值在两个字典存在吗?
- 打印不在其他字典中出现的值。
- 使用键,字典中匹配的值以及将字典打印到屏幕的值创建新词典。
我可以搜索这两个字典并找到69个不同的条目。
我的问题是:
- 我怀疑有这样做的,
- 的更简单的方法,我总怀疑有更好的方法。
- 我没有创建第三个字典与匹配的两个值/键。
目前代码:
#!/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)