Home >Backend Development >Python Tutorial >python list转dict示例分享

python list转dict示例分享

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 11:29:211298browse

需求:['1:a','2:b','3:c'] 转换为 {'1′: 'a','3′: 'c','2′: ''}

代码如下:


a = {}
b = ['1:a','2:b','3:c']
map(lambda x:a.setdefault(x.split(':')[0], x.split(':')[1]), b)
print a
{'1': 'a', '3': 'c', '2': 'b'}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn