我知道这很简单,但我无法理解。 我想将字典列表中的所有值转换为小写字母:
{'John greased ': ['Axle', 'wheel', 'wheels', 'wheel', 'enGine', ''],
'Maria testa': ['teste', 'teste', '', '', '', ''],
'Paul alleged ': ['truth', 'crime', 'facts', 'infidelity', 'incident', ''],
'Tracy freed ': ['animals', 'fish', 'slaves', 'slaves', 'slaves', 'pizza'],
'Lisa plowed ': ['field', 'Field', 'FIELD', 'bola', '', '']}
我尝试使用:
low = {k.lower():v.lower() for k, v in result.items()}
print(low)
但它不起作用。关于如何解决这个问题的任何建议?非常感谢你!
答案 0 :(得分:4)
您的值是列表,因此请添加列表解析:
{k.lower(): [i.lower() for i in v] for k, v in result.items()}
演示:
>>> result = {'John greased ': ['Axle', 'wheel', 'wheels', 'wheel', 'enGine', ''],
... 'Maria testa': ['teste', 'teste', '', '', '', ''],
... 'Paul alleged ': ['truth', 'crime', 'facts', 'infidelity', 'incident', ''],
... 'Tracy freed ': ['animals', 'fish', 'slaves', 'slaves', 'slaves', 'pizza'],
... 'Lisa plowed ': ['field', 'Field', 'FIELD', 'bola', '', '']}
>>> {k.lower(): [i.lower() for i in v] for k, v in result.items()}
{'lisa plowed ': ['field', 'field', 'field', 'bola', '', ''], 'tracy freed ': ['animals', 'fish', 'slaves', 'slaves', 'slaves', 'pizza'], 'paul alleged ': ['truth', 'crime', 'facts', 'infidelity', 'incident', ''], 'maria testa': ['teste', 'teste', '', '', '', ''], 'john greased ': ['axle', 'wheel', 'wheels', 'wheel', 'engine', '']}
>>> from pprint import pprint
>>> pprint({k.lower(): [i.lower() for i in v] for k, v in result.items()})
{'john greased ': ['axle', 'wheel', 'wheels', 'wheel', 'engine', ''],
'lisa plowed ': ['field', 'field', 'field', 'bola', '', ''],
'maria testa': ['teste', 'teste', '', '', '', ''],
'paul alleged ': ['truth', 'crime', 'facts', 'infidelity', 'incident', ''],
'tracy freed ': ['animals', 'fish', 'slaves', 'slaves', 'slaves', 'pizza']}