key = 'take'
val = 5
dicts = [
OrderedDict([
(u'rt', 0),
(u'janoameezy', 0),
(u'calum', 0),
(u'is', 0),
(u'me', 0),
(u'whenever', 0),
(u'i', 0),
(u'take', 0),
(u'a', 0),
(u'selfie', 0),
(u'http', 0),
(u't', 0),
(u'co', 0),
(u'sxn68vta2a', 0)
]),
OrderedDict([
(u'relax', 0),
(u'and', 0),
(u'take', 0),
(u'notes', 0),
(u'while', 0),
(u'i', 0),
(u'tokes', 0),
(u'of', 0),
(u'the', 0),
(u'marijuana', 0),
(u'smoke', 0)
])
]
如何用 val 替换 key 的所有实例?
最佳答案
您只需循环浏览列表即可:
for od in dicts:
if key in od:
od[key] = val
od 中的 key
首先测试成员资格;仅当 key 之前存在时才会更新。
关于python - 用Python中的值更新字典列表中所有匹配的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24589539/