多个字符串到dict转换

时间:2019-03-21 23:06:39

标签: dictionary

如何从此字符串创建字典: “约翰:23,马克:24,蒂姆:43,杰克:21”

预期输出: dict = {“ John”:23,“ Marc”:24,“ Tim”:43,“ Jake”:21} 所以我可以做: sum(dict.values())

谢谢

1 个答案:

答案 0 :(得分:0)

在python中:

>>> x = "John:23,Marc:24,Tim:43,Jake:21
>>> dict([y.split(':') for y in x.split(',')])
{'Tim': '43', 'John': '23', 'Jake': '21', 'Marc': '24'}
>>>