【发布时间】:2019-02-01 16:45:42
【问题描述】:
我想将 dictionary_values 中的空列表替换为随机字符,例如“a”。
我的意见:
k = [{'1': 'man'},
{'8': []},
{'3': 'chester'},
{'5': 'united'},
{'7': 'one'},
{'0': 'man'},
{'2': 'army'},
{'4': 'my'},
{'6': 'random'},
{'9': 'example'},
{'1': 'for'},
{'8': []},
{'3': 'asking'},
{'5': 'in'},
{'7': 'stack'},
{'0': 'over'},
{'2': 'flow'},
{'4': 'expert'},
{'6': 'people'},
{'9': 'yes'}]
我当前的代码:
for i in k:
for u, l in i.items():
if "[]" in l:
l.replace("[]", "a")
print(l)
我尝试运行此代码但没有得到任何输出。如果可能,我想将所有“[]”替换为“a”。有可能实现这一点吗?
【问题讨论】:
标签: python list dictionary replace