Python:将'key:value'元素的列表解析为'key'的字典:值对

Jas*_*pel 2 python dictionary list

我有以下列表:

['a:1', 'b:2', 'c:3', 'd:4']
Run Code Online (Sandbox Code Playgroud)

我想转换为有序的dict(使用collections):

{'a': 1, 'b': 2, 'c': 3, 'd': 4}
Run Code Online (Sandbox Code Playgroud)

我在这里看过使用正则表达式的解决方案,但我不熟悉正则表达式,足以推出解决方案.有什么想法吗?

Jon*_*nts 10

d = collections.OrderedDict(el.split(':') for el in your_list)
Run Code Online (Sandbox Code Playgroud)

或者,将值转换为整数:

OrderedDict( (k, int(v)) for k, v in (el.split(':') for el in your_list))
Run Code Online (Sandbox Code Playgroud)