【问题标题】:Check if a value from a list of values in a python dictionary exists in another dictionary检查python字典中值列表中的值是否存在于另一个字典中
【发布时间】:2017-11-15 15:31:09
【问题描述】:

我编写了一个 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 甚至不存在于 Master 中,PersonReport 包含一个无效的字段 dob,因为它不在 Master 中。唯一有效的报告是TransactionReport

所以我的想法是将这些 csvs 作为字典读取,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']}

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

所以我试试:

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

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

【问题讨论】:

  • 发布预期输出
  • 已经完成,谢谢@RomanPerekhrest

标签: python python-3.x csv dictionary


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你可以试试这个:

    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)
    

    输出:

    [' TransactionReport in dict', ' PersonReport in dict', ' AccountReport not in dict']
    [[' dob is an invalid value']]
    

    【讨论】:

      【解决方案3】:

      您的代码有几个错误导致您检查每个字段。使用continue 并仅遍历正确的密钥即可解决此问题。 这是一个固定的代码sn-p:

      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))
      

      这个输出:

       dob is an invalid field in  PersonReport report
       AccountReport is an invalid report
      Success finding valid  TransactionReport report in master
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-09-16
        • 2015-09-24
        • 2019-02-13
        • 1970-01-01
        • 2022-01-10
        • 1970-01-01
        • 1970-01-01
        • 2018-12-08
        相关资源
        最近更新 更多