python - 检查 python 字典中的值列表中的值是否存在于另一个字典中

标签 python python-3.x csv dictionary

我编写了一个 python 脚本来验证报告模板。有一个主模板,其中包含所有可能的报告及其所有可能的字段。然后有一个 csv 模板,其中包含其中一些报告。为了简单起见,让我们说看起来像这样;

模板 csv:

输出名称、col1、col2、col3、col4、col5 PersonReport,姓名,年龄,出生日期,ID 账户报告、f1、f2、f3、f4、f5 交易报告,f1,f2,f3,f4,f5

主 csv:

输出名称、col1、col2、col3、col4、col5 PersonReport,姓名,姓氏,街道,年龄,ID 交易报告,f1,f2,f3,f4,f5

因此,在此示例中,AccountReport 甚至不存在于 Master 中,PersonReport 包含一个无效的字段 dob因为它不在师父之中。唯一有效的报告是 TransactionReport

所以我的想法是将这些 csv 作为字典读取,以 OutputName 作为键,以字段名称作为值。

 import pandas as pd
 masterDf = pd.read_csv('master.csv')
 master = masterDf.set_index('OutputName').T.to_dict('list')

 templateDf = pd.read_csv('template.csv')
 template = templateDf.set_index('OutputName').T.to_dict('list')

字典看起来像;

template = {'PersonReport': ['姓名', '姓氏', '年龄', '出生日期', 'id'], 'AccountReport': ['f1', 'f2', 'f3 ', 'f4', 'f5'], 'TransactionReport': ['f1', 'f2', 'f3', 'f4', 'f5']}

master = {'PersonReport': ['姓名', '姓氏', '街道', '年龄', 'id'], 'TransactionReport': ['f1', 'f2', 'f3 ', 'f4', 'f5']}

现在我想首先匹配键,以找出主字典中不存在哪些键。之后,当查找匹配的键时,我希望通过检查它们是否存在于主字典的值中来检查字典中的值是否有效。

所以我尝试:

errorCount = 0

for key, value in template.items():
    if key  not in master:
        print("{} is an invalid report".format(key))
        errorCount += 1
    if key in master:
        for fields in template.values():
            for field in fields:
                for cols in master.values():
                    if field not in cols:
                        print("{} is an invalid field in {} report".format(field, key))
                        errorCount += 1

但是我得到的输出是错误的。我得到:

name is an invalid field in  PersonReport report
 surname is an invalid field in  PersonReport report
 age is an invalid field in  PersonReport report
 dob is an invalid field in  PersonReport report
 dob is an invalid field in  PersonReport report
 id is an invalid field in  PersonReport report
 f1 is an invalid field in  PersonReport report
 f2 is an invalid field in  PersonReport report
 f3 is an invalid field in  PersonReport report
 f4 is an invalid field in  PersonReport report
 f5  is an invalid field in  PersonReport report
 f5  is an invalid field in  PersonReport report
 f1 is an invalid field in  PersonReport report
 f2 is an invalid field in  PersonReport report
 f3 is an invalid field in  PersonReport report
 f4 is an invalid field in  PersonReport report
 f5 is an invalid field in  PersonReport report
 AccountReport is an invalid report
 name is an invalid field in  TransactionReport report
 surname is an invalid field in  TransactionReport report
 age is an invalid field in  TransactionReport report
 dob is an invalid field in  TransactionReport report
 dob is an invalid field in  TransactionReport report
 id is an invalid field in  TransactionReport report
 f1 is an invalid field in  TransactionReport report
 f2 is an invalid field in  TransactionReport report
 f3 is an invalid field in  TransactionReport report
 f4 is an invalid field in  TransactionReport report
 f5  is an invalid field in  TransactionReport report
 f5  is an invalid field in  TransactionReport report
 f1 is an invalid field in  TransactionReport report
 f2 is an invalid field in  TransactionReport report
 f3 is an invalid field in  TransactionReport report
 f4 is an invalid field in  TransactionReport report
 f5 is an invalid field in  TransactionReport report

我的预期输出是:

AccountReport is an invalid report
dob is an invalid field in PersonReport report

任何帮助表示赞赏 谢谢 p.s我使用的是python 3.6

最佳答案

我保持简单并遵循您的约定:

errorCount = 0

for key in template.keys():
    if key not in master:
        print("{} is an invalid report".format(key))
        errorCount += 1
    else:
        if template[key] != master[key]:
            fields = [f for f in template[key] if f not in master[key]]
            for f in fields:
                print("{} is an invalid field in {} report".format(f, key))
                errorCount += 1

从控制台:

AccountReport is an invalid report
dob is an invalid field in  PersonReport report

关于python - 检查 python 字典中的值列表中的值是否存在于另一个字典中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47311315/

相关文章:

python - 合并可变数量的嵌套字典的通用函数

python - 如何从根路径自动重定向?

python - “NoneType”对象没有属性 'config'

python-3.x - 函数运行时 Python Tkinter 禁用按钮

python - 如何让 DictReader 打开以分号作为字段分隔符的文件?

c - 从 C 中的 CSV 表获取值

python - 如何在Heroku上安装python opencv?

c++ - 使用 C++ 扩展 Python/Numpy,模块在初始化时崩溃

python - Python 中的 *tuple 和 **dict 是什么意思?

python - 内存错误使用openpyxl和大数据excel