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

2024-09-29 23:27:22 发布

您现在位置:Python中文网/ 问答频道 /正文

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

模板csv:

OutputName, col1, col2, col3, col4, col5 PersonReport, name, surname, age, dob, id AccountReport, f1, f2, f3, f4, f5 TransactionReport, f1, f2, f3, f4, f5

主csv:

OutputName, col1, col2, col3, col4, col5 PersonReport, name, surname, street, age, id TransactionReport, f1, f2, f3, f4, f5

所以在这个例子中,AccountReport甚至不存在于主节点中,PersonReport包含一个字段{},因为它不在主节点中。唯一有效的报告是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': [' name', ' surname', ' age', ' dob', ' id'], ' AccountReport': [' f1', ' f2', ' f3', ' f4', ' f5 '], ' TransactionReport': [' f1', ' f2', ' f3', ' f4', ' f5']}

master = {' PersonReport': [' name', ' surname', ' street', ' age', ' id'], ' TransactionReport': [' f1', ' f2', ' f3', ' f4', ' f5']}

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

所以我试着:

^{pr2}$

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

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、 使用python I的6.6 m


Tags: csvinreportanfieldisf5f2
3条回答

你可以试试这个:

template = {' PersonReport': [' name', ' surname', ' age', ' dob', ' id'], ' AccountReport': [' f1', ' f2', ' f3', ' f4', ' f5 '], ' TransactionReport': [' f1', ' f2', ' f3', ' f4', ' f5']}
master = {' PersonReport': [' name', ' surname', ' street', ' age', ' id'], ' TransactionReport': [' f1', ' f2', ' f3', ' f4', ' f5']}
invalid_names = ["{} in dict".format(i) if i in master else "{} not in dict".format(i) for i in template]
invalid_values = filter(lambda x:x, [["{} is an invalid value".format(c) for c in b if c not in master[a]] for a, b in template.items() if a in master])
print(invalid_names)
print(invalid_values)

输出:

^{pr2}$

我保持简单,遵循你们的惯例:

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

从控制台:

^{pr2}$

你的代码有几个错误导致你检查每个字段。使用continue并只迭代正确的键就解决了这个问题。 下面是一个固定的代码片段:

for key in template.keys():
    if key not in master:
        print("{} is an invalid report".format(key))
        errorCount += 1
        continue # Continue to next key
    cols = master[key] # Get the correct column names from the current key
    errorFlag = False
    for field in template[key]:
        if field not in cols:
            print("{} is an invalid field in {} report".format(field, key)) 
            errorCount += 1
            errorFlag = True # You can break here if you wish not to keep incrementing the errorCount.

    if not errorFlag: # We did not find any bad column
        print("Success finding valid {} report in master".format(key))

该输出:

^{pr2}$

相关问题 更多 >

    热门问题