我有以下字典。对于值为['FAIL', 'PASS']
的对象,我只想将其替换为'PASS'
。
Dict = [('Main Menu-046', ['PASS']), ('Main Menu-047', ['FAIL']), ('Main Menu-044', ['FAIL', 'PASS']), ('Main Menu-045', ['PASS']), ('Main Menu-042', ['FAIL', 'PASS']), ('Main Menu-043', ['FAIL'])
我尝试了以下内容:
if "FAIL" in [x for v in Dict.values() for x in v]: ## if true
Dict.values == "PASS"
答案 0 :(得分:1)
如果我正确理解了您的问题,您希望将['FAIL', 'PASS']
替换为['PASS']
processed_list = [(x[0], ["PASS"] if "PASS" in x[1] else x[1]) for x in Dict]
结果:
[('Main Menu-046', ['PASS']),
('Main Menu-047', ['FAIL']),
('Main Menu-044', ['PASS']),
('Main Menu-045', ['PASS']),
('Main Menu-042', ['PASS']),
('Main Menu-043', ['FAIL'])]
答案 1 :(得分:0)
您不使用dict,而是使用(Main, [PASS/FAIL])
如果您需要字典,可以d = { key:value, key:value ... }
或d = dict()
创建字典,只需d['newkey'] = value
另外,如果您愿意,可以像这样应用默认值:
from collections import defaultdict
d = defaultdict(lambda : defaultvalue)
当你有你的词典时,尝试一个循环:
for key in d.keys():
if d[key] == 'FAIL': # or if 'FAIL' in d[key] if d[key] is a list like ['FAIL']
d[key] = 'PASS' # or == ? why a boolean without condition or return ?