我正在寻找一种使用lambda或列表理解来更改列表中字典中的值的方法
假设我有一个简单的字典列表
list_of_objects = [
{'note': 'note1', 'comments': 'Test comments', elem': 1},
{'note': 'note2','comments': None, elem': 2}
]
我需要遍历这个列表,找到每个dict中的每个值,并将其替换为文本“未提供任何注释”
预期结果:
[{'note': 'note1',
'comments': 'Test comments'
'elem': 1},
{'note': 'note2',
'comments': 'No comments was provided'
'elem': 2}]
我可以使用标准for循环操作来完成。但是我正在寻找一个机会,我们lambda或列表理解,以尽量减少我的代码
见下文(我认为for循环带来更好的可读性)
输出
相关问题 更多 >
编程相关推荐