<p>您可以使用<code>dict.update</code></p>
<pre class="lang-py prettyprint-override"><code>data = [
{
"id": "john",
"title": "tech",
"date": "2020/1/1",
"shift": "DAYS"
},
{
"id": "tom",
"title": "tech",
"date": "2021/5/5",
"shift": "NIGHT"
}
]
shift_mapping = {
'DAYS': { # or DAY whatever you have
"shiftHourStart": "22:00:00",
"shiftHourEnd": "06:00:00"
},
'NIGHT':{
"shiftHourStart": "06:00:00",
"shiftHourEnd": "15:00:00",
}
}
for each_shift in data:
if 'shift' in each_shift:
each_shift.update(shift_mapping[each_shift['shift']])
else:
print('Warning!! shift key does not exists!')
print(data)
</code></pre>
<pre><code>[{'id': 'john',
'title': 'tech',
'date': '2020/1/1',
'shift': 'DAYS',
'shiftHourStart': '22:00:00',
'shiftHourEnd': '06:00:00'},
{'id': 'tom',
'title': 'tech',
'date': '2021/5/5',
'shift': 'NIGHT',
'shiftHourStart': '06:00:00',
'shiftHourEnd': '15:00:00'}]
</code></pre>