Convert list of strings to dictionary

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

    a = ['Tests run: 1', ' Failures: 0', ' Errors: 0']
    b = dict([i.split(': ') for i in a])
    final = dict((k, int(v)) for k, v in b.items())  # or iteritems instead of items in Python 2
    print(final)
    

    Result

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

提交回复
热议问题