Python脚本中用于比较两个列表的逻辑错误

2024-04-25 04:21:34 发布

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

我需要比较一个配置列表(req\u config)和一个预先存在的(masterList)列表。你知道吗

我得到一些逻辑错误,因为代码在某些配置中运行良好,而在其他配置中给出错误的输出。请帮忙。你知道吗

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

Tags: noconfigformatchwidthheightmatchingmaxlevel
1条回答
网友
1楼 · 发布于 2024-04-25 04:21:34

最佳做法是以某种形式规范化输入,例如,在本例中,您可以使用str.lower()将大小写混合字符转换为小写字符,然后执行比较:

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

相关问题 更多 >