字典 - 如何为键和值的特定值添加条件?
Dictionary - how to add condition for specific value of key and value?
my_dict = {
"camel": "ok",
"test_environments": [
{
"Branch Coverage": "-",
"ASIL": "ASIL-D",
"Test Environment": "APCOM",
"Test Configuration": "Vehicle",
"Statement Coverage": "-",
"Test Status": "36/36(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
},
"Branch Coverage": "-",
"ASIL": "ASIL-B",
"Test Environment": "AP",
"Test Configuration": "Per",
"Statement Coverage": "-",
"Test Status": "20/20(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
}
]
}
我想做这样的事情:如果任何键的值为'-'并且ASIL的值为ASIL - D,将'Branch Coverage'键拆分为两个键并将它们分配给0。分支覆盖将变为:“通过的分支机构覆盖范围”:“0”和“涵盖的分支机构覆盖范围”:“0”
我正在尝试使用这段代码,但我不知道如何添加附加条件,即仅当 ASIL 为 ASIL 时这些值才变为 0 - D:
for index, _ in enumerate(my_dict['test_environments']):
for key, values in my_dict['test_environments'][index].items():
new_key = key.replace(" ", "_").lower()
if '-' in values:
value1 = 0
value2 = 0
my_dict['test_environments'][index]['branch_coverage' + '_passed'] = value1
my_dict['test_environments'][index]['branch_coverage' + '_covered'] = value2
我不确定你到底想要什么。如果你想用 0 替换'-',你可以像这样循环:
my_dict = {
"camel": "ok",
"test_environments": [
{
"Branch Coverage": "-",
"ASIL": "ASIL-D",
"Test Environment": "APCOM",
"Test Configuration": "Vehicle",
"Statement Coverage": "-",
"Test Status": "36/36(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
}
]
}
for dic in my_dict['test_environments']:
for key in dic:
if dic[key] == '-':
dic[key] = 0
for dic in dictionary:
for key in dic:
if dic[key] == '-':
dic[key] = 0
IIUC,你可以使用:
选项 1:用新条目替换旧条目
for index, env in enumerate(my_dict['test_environments']):
if env['ASIL'] == 'ASIL-D':
data = {}
for key, val in env.items():
if env[key] == '-':
data[f"{key} Passed"] = 0
data[f"{key} Convered"] = 0
else:
data[key] = val
my_dict['test_environments'][index] = data.copy()
print(my_dict)
# Output
'test_environments': [{'Branch Coverage Passed': 0,
'Branch Coverage Convered': 0,
'ASIL': 'ASIL-D',
'Test Environment': 'APCOM',
'Test Configuration': 'Vehicle',
'Statement Coverage Passed': 0,
'Statement Coverage Convered': 0,
'Test Status': '36/36(100%)',
'Type of Code': 'handwritten',
'MC/DC Coverage Passed': 0,
'MC/DC Coverage Convered': 0,
'Verified SW Units': 'APCOM'}]}
选项 2:向当前条目添加新值
for env in my_dict['test_environments']:
if env['ASIL'] == 'ASIL-D':
data = {}
for key, val in env.items():
if env[key] == '-':
data[f"{key} Passed"] = 0
data[f"{key} Convered"] = 0
else:
data[key] = val
env.update(data)
print(my_dict)
# Output
{'camel': 'ok',
'test_environments': [{'Branch Coverage': '-',
'ASIL': 'ASIL-D',
'Test Environment': 'APCOM',
'Test Configuration': 'Vehicle',
'Statement Coverage': '-',
'Test Status': '36/36(100%)',
'Type of Code': 'handwritten',
'MC/DC Coverage': '-',
'Verified SW Units': 'APCOM',
'Branch Coverage Passed': 0,
'Branch Coverage Convered': 0,
'Statement Coverage Passed': 0,
'Statement Coverage Convered': 0,
'MC/DC Coverage Passed': 0,
'MC/DC Coverage Convered': 0}]}
这确实是你所说的你想要发生的事情(我认为):
示例数据:
my_dict = {
"camel": "ok",
"test_environments": [
{
"Branch Coverage": "-",
"ASIL": "ASIL-D",
"Test Environment": "APCOM",
"Test Configuration": "Vehicle",
"Statement Coverage": "-",
"Test Status": "36/36(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
},
{
"Branch Coverage": "-",
"ASIL": "ASIL-B",
"Test Environment": "AP",
"Test Configuration": "Per",
"Statement Coverage": "-",
"Test Status": "20/20(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
}
]
}
代码:
import json # For printing result.
for test_env in my_dict['test_environments']:
if(test_env['ASIL'] == 'ASIL-D' and
any(['-' in value for value in test_env.values()])):
for key, value in test_env.items():
if value == '-':
test_env['branch_coverage' + '_passed'] = 0
test_env['branch_coverage' + '_covered'] = 0
print('Result:')
print(json.dumps(my_dict, indent=4, sort_keys=True))
输出:
Result:
{
"camel": "ok",
"test_environments": [
{
"ASIL": "ASIL-D",
"Branch Coverage": "-",
"MC/DC Coverage": "-",
"Statement Coverage": "-",
"Test Configuration": "Vehicle",
"Test Environment": "APCOM",
"Test Status": "36/36(100%)",
"Type of Code": "handwritten",
"Verified SW Units": "APCOM",
"branch_coverage_covered": 0,
"branch_coverage_passed": 0
},
{
"ASIL": "ASIL-B",
"Branch Coverage": "-",
"MC/DC Coverage": "-",
"Statement Coverage": "-",
"Test Configuration": "Per",
"Test Environment": "AP",
"Test Status": "20/20(100%)",
"Type of Code": "handwritten",
"Verified SW Units": "APCOM"
}
]
}
my_dict = {
"camel": "ok",
"test_environments": [
{
"Branch Coverage": "-",
"ASIL": "ASIL-D",
"Test Environment": "APCOM",
"Test Configuration": "Vehicle",
"Statement Coverage": "-",
"Test Status": "36/36(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
},
"Branch Coverage": "-",
"ASIL": "ASIL-B",
"Test Environment": "AP",
"Test Configuration": "Per",
"Statement Coverage": "-",
"Test Status": "20/20(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
}
]
}
我想做这样的事情:如果任何键的值为'-'并且ASIL的值为ASIL - D,将'Branch Coverage'键拆分为两个键并将它们分配给0。分支覆盖将变为:“通过的分支机构覆盖范围”:“0”和“涵盖的分支机构覆盖范围”:“0”
我正在尝试使用这段代码,但我不知道如何添加附加条件,即仅当 ASIL 为 ASIL 时这些值才变为 0 - D:
for index, _ in enumerate(my_dict['test_environments']):
for key, values in my_dict['test_environments'][index].items():
new_key = key.replace(" ", "_").lower()
if '-' in values:
value1 = 0
value2 = 0
my_dict['test_environments'][index]['branch_coverage' + '_passed'] = value1
my_dict['test_environments'][index]['branch_coverage' + '_covered'] = value2
我不确定你到底想要什么。如果你想用 0 替换'-',你可以像这样循环:
my_dict = {
"camel": "ok",
"test_environments": [
{
"Branch Coverage": "-",
"ASIL": "ASIL-D",
"Test Environment": "APCOM",
"Test Configuration": "Vehicle",
"Statement Coverage": "-",
"Test Status": "36/36(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
}
]
}
for dic in my_dict['test_environments']:
for key in dic:
if dic[key] == '-':
dic[key] = 0
for dic in dictionary:
for key in dic:
if dic[key] == '-':
dic[key] = 0
IIUC,你可以使用:
选项 1:用新条目替换旧条目
for index, env in enumerate(my_dict['test_environments']):
if env['ASIL'] == 'ASIL-D':
data = {}
for key, val in env.items():
if env[key] == '-':
data[f"{key} Passed"] = 0
data[f"{key} Convered"] = 0
else:
data[key] = val
my_dict['test_environments'][index] = data.copy()
print(my_dict)
# Output
'test_environments': [{'Branch Coverage Passed': 0,
'Branch Coverage Convered': 0,
'ASIL': 'ASIL-D',
'Test Environment': 'APCOM',
'Test Configuration': 'Vehicle',
'Statement Coverage Passed': 0,
'Statement Coverage Convered': 0,
'Test Status': '36/36(100%)',
'Type of Code': 'handwritten',
'MC/DC Coverage Passed': 0,
'MC/DC Coverage Convered': 0,
'Verified SW Units': 'APCOM'}]}
选项 2:向当前条目添加新值
for env in my_dict['test_environments']:
if env['ASIL'] == 'ASIL-D':
data = {}
for key, val in env.items():
if env[key] == '-':
data[f"{key} Passed"] = 0
data[f"{key} Convered"] = 0
else:
data[key] = val
env.update(data)
print(my_dict)
# Output
{'camel': 'ok',
'test_environments': [{'Branch Coverage': '-',
'ASIL': 'ASIL-D',
'Test Environment': 'APCOM',
'Test Configuration': 'Vehicle',
'Statement Coverage': '-',
'Test Status': '36/36(100%)',
'Type of Code': 'handwritten',
'MC/DC Coverage': '-',
'Verified SW Units': 'APCOM',
'Branch Coverage Passed': 0,
'Branch Coverage Convered': 0,
'Statement Coverage Passed': 0,
'Statement Coverage Convered': 0,
'MC/DC Coverage Passed': 0,
'MC/DC Coverage Convered': 0}]}
这确实是你所说的你想要发生的事情(我认为):
示例数据:
my_dict = {
"camel": "ok",
"test_environments": [
{
"Branch Coverage": "-",
"ASIL": "ASIL-D",
"Test Environment": "APCOM",
"Test Configuration": "Vehicle",
"Statement Coverage": "-",
"Test Status": "36/36(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
},
{
"Branch Coverage": "-",
"ASIL": "ASIL-B",
"Test Environment": "AP",
"Test Configuration": "Per",
"Statement Coverage": "-",
"Test Status": "20/20(100%)",
"Type of Code": "handwritten",
"MC/DC Coverage": "-",
"Verified SW Units": "APCOM"
}
]
}
代码:
import json # For printing result.
for test_env in my_dict['test_environments']:
if(test_env['ASIL'] == 'ASIL-D' and
any(['-' in value for value in test_env.values()])):
for key, value in test_env.items():
if value == '-':
test_env['branch_coverage' + '_passed'] = 0
test_env['branch_coverage' + '_covered'] = 0
print('Result:')
print(json.dumps(my_dict, indent=4, sort_keys=True))
输出:
Result:
{
"camel": "ok",
"test_environments": [
{
"ASIL": "ASIL-D",
"Branch Coverage": "-",
"MC/DC Coverage": "-",
"Statement Coverage": "-",
"Test Configuration": "Vehicle",
"Test Environment": "APCOM",
"Test Status": "36/36(100%)",
"Type of Code": "handwritten",
"Verified SW Units": "APCOM",
"branch_coverage_covered": 0,
"branch_coverage_passed": 0
},
{
"ASIL": "ASIL-B",
"Branch Coverage": "-",
"MC/DC Coverage": "-",
"Statement Coverage": "-",
"Test Configuration": "Per",
"Test Environment": "AP",
"Test Status": "20/20(100%)",
"Type of Code": "handwritten",
"Verified SW Units": "APCOM"
}
]
}