Convert list of strings to dictionary

前端 未结 7 930
深忆病人
深忆病人 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:13

    Try this

    In [35]: a = ['Tests run: 1', ' Failures: 0', ' Errors: 0']
    
    In [36]: {i.split(':')[0]: int(i.split(':')[1]) for i in a}
    Out[36]: {'Tests run': 1, ' Failures': 0, ' Errors': 0}
    
    In [37]:
    

提交回复
热议问题