我有一个词典列表,并希望从键值#34;质量"中减去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]
但没有奏效
提前感谢您的帮助!
答案 0 :(得分:2)
如何采取直截了当的方法:
>>> 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'}]
答案 1 :(得分:1)
for dict_ in list_:
dict_['quality'] -= 1
尝试使用列表理解进行单行处理没有任何好处。