使用循环来遍历列表中的每个字典,并在每个字典中添加一个键值对 'flag': 0。

代码示例:

list_of_dicts = [{'image':1,'label':1},{'image':2,'label':2},{'image':3,'label':3}]
for dictionary in list_of_dicts:
    dictionary['flag'] = 0

print(list_of_dicts)

输出结果:

[{'image': 1, 'label': 1, 'flag': 0}, {'image': 2, 'label': 2, 'flag': 0}, {'image': 3, 'label': 3, 'flag': 0}]
Python 字典列表添加新键值对:'flag': 0

原文地址: https://www.cveoy.top/t/topic/ixgh 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录