本文介绍了Python读取两个字典比较值并创建第三个字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个要比较的字典文件.我需要知道另一个字典中是否出现了一个字典中的值.键是不一样的,所以我不能基于键.

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.

目标:

  1. 两个词典中是否存在相同的值?
  2. 打印其他词典中未出现的值.
  3. 使用关键字"创建新的词典,这些关键字来自词典中与之匹配的值,并将词典显示在屏幕上.

我能够搜索两个词典并找到69个不同的条目.

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

我的问题是:

  1. 我怀疑这样做有更简单的方法,并且
  2. 我怀疑总体上有更好的方法.
  3. 在匹配时,我不会使用两个字典中的值/键来创建第三个字典.
  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.

当前代码:

#!/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)

推荐答案

如果只考虑值,则可能需要 dict.values()方法.

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)

这篇关于Python读取两个字典比较值并创建第三个字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 04:59