需求:[‘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’}
A programmer’s note boke for work and life
需求:[‘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’}