Convert list of strings to dictionary

前端 未结 7 872
深忆病人
深忆病人 2020-12-01 22:08

I have a list

[\'Tests run: 1\', \' Failures: 0\', \' Errors: 0\']

I would like to convert it to a dictionary as

{\'Tests r         


        
7条回答
  •  攒了一身酷
    2020-12-01 22:29

    l = ['Tests run: 1', ' Failures: 0', ' Errors: 0']
    d = dict([map(str.strip, i.split(':')) for i in l])
    for key, value in d.items():
        d[key] = int(value)
    print(d)
    

    output:

    {'Tests run': 1, 'Errors': 0, 'Failures': 0}
    

提交回复
热议问题