用于比较两个列表的 Python 脚本中的逻辑错误 [英] Logical Error in Python script for comparing two lists

查看:18
本文介绍了用于比较两个列表的 Python 脚本中的逻辑错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将配置列表(req_config) 与预先存在的(masterList) 列表进行比较.

I need to compare a configuration list(req_config) with a pre-existing(masterList) list.

我遇到了一些逻辑错误,因为代码对于某些配置工作正常,而对于其他配置则给出错误输出.请帮忙.

I am getting some logical Error, as code is working fine for some configs and giving false output for other configs. Kindly help.

import re
masterList = ['MEMSize', 'conservativeRasEn', 'Height', 'multipleBatch', 'Width', 'dumpAllRegsAtFinish', 'ProtectCtl', 'isdumpEnabled','vh0', 'vw0','lEnable', 'WaveSize','maxLevel','pmVer', 'cSwitchEn', 'disablePreempt', 'disablePreemptInPass', 'ctxSwQueryEn', 'forceEnable', 'enableDebug', '5ErrEn', 'ErrorEn']
req_config = ['MEMSize', 'Height', 'Width', 'isdumpEnabled', 'vh0', 'vw0', 'lEnable', 'WaveSize', 'maxLevel', 'Information', 'ConservativeRasEn']

for config in req_config:
    if any(config in s for s in masterList):
        print "Config matching: ", config
    else:
        print "No match for: ", config

预期输出:

Config matching:  MEMSize
Config matching:  Height
Config matching:  Width
Config matching:  isdumpEnabled
Config matching:  vh0
Config matching:  vw0
Config matching:  lEnable
Config matching:  WaveSize
Config matching:  maxLevel
No match for:  Information
Config matching:  ConservativeRasEn

当前输出:

Config matching:  MEMSize
Config matching:  Height
Config matching:  Width
Config matching:  isdumpEnabled
Config matching:  vh0
Config matching:  vw0
Config matching:  lEnable
Config matching:  WaveSize
Config matching:  maxLevel
No match for:  Information
No match for:  ConservativeRasEn

推荐答案

最佳实践是以某种形式标准化输入,例如在这种情况下,您可以使用 str.lower()将混合大小写字符转换为小写字符,然后进行比较:

The best practise is to normalize the inputs in some form, for example in this case, you may use str.lower() to convert the mixed case characters to lowercase characters and then perform comparison:

masterList = ['MEMSize', 'conservativeRasEn', 'Height', 'multipleBatch', 'Width', 'dumpAllRegsAtFinish', 'ProtectCtl', 'isdumpEnabled','vh0', 'vw0','lEnable', 'WaveSize','maxLevel','pmVer', 'cSwitchEn', 'disablePreempt', 'disablePreemptInPass', 'ctxSwQueryEn', 'forceEnable', 'enableDebug', '5ErrEn', 'ErrorEn']
req_config = ['MEMSize', 'Height', 'Width', 'isdumpEnabled', 'vh0', 'vw0', 'lEnable', 'WaveSize', 'maxLevel', 'Information', 'ConservativeRasEn']

masterList = map(str.lower, masterList)

for config in req_config:
    if config.lower() in masterList:
        print "Config matching: ", config
    else:
        print "No match for: ", config

这篇关于用于比较两个列表的 Python 脚本中的逻辑错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆