【问题标题】:Python read two dictionaries compare values and create third dictionaryPython读取两个字典比较值并创建第三个字典
【发布时间】:2016-11-07 15:15:41
【问题描述】:

我有两个要比较的字典文件。我需要知道一个字典中的值是否出现在另一个字典中。钥匙不一样,所以我不能根据钥匙去。

目标:

  1. 两个字典中是否存在相同的值?
  2. 打印其他字典中没有出现的值。
  3. 使用键、匹配的字典中的值创建一个新字典,并将字典打印到屏幕。

我可以同时搜索这两个字典并找到 69 个不同的条目。

我的问题是:

  1. 我怀疑有一种更简单的方法可以做到这一点,并且
  2. 我怀疑总体上有更好的方法。
  3. 当匹配时,我不会使用两者中的值/键创建第三个字典。

当前代码:

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

【问题讨论】:

    标签: python dictionary compare


    【解决方案1】:

    如果您只关心值,dict.values() 方法可能就是您想要的。

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

    我不确定您的第三个要求究竟是什么。是这样的吗?

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-04
      • 2018-04-06
      • 1970-01-01
      相关资源
      最近更新 更多