我有一个字典列表,想从列表中所有字典的键“质量”的值中减去1
list=[{key1:xx, key2:xx, quality:5},{key1:xx, key2:xx, quality:5},{key1:xx, key2:xx, quality:5}]
并希望得到
list=[{key1:xx, key2:xx, quality:4},{key1:xx, key2:xx, quality:4},{key1:xx, key2:xx, quality:4}]
我试过了
list=[(i['quality']-1 for i in list]
但是没用
在此先感谢您的帮助!
最佳答案
采取直接方法怎么样:
>>> l = [{'a':'aa', 'b':'bb', 'quality':5},{'c':'cc', 'd':'dd', 'quality':5},{'e':'ee', 'f':'ff', 'quality':5}]
>>> for item in l:
... item['quality'] -= 1
...
>>> l
[{'a': 'aa', 'b': 'bb', 'quality': 4}, {'c': 'cc', 'quality': 4, 'd': 'dd'}, {'quality': 4, 'e': 'ee', 'f': 'ff'}]