Creating a dictionary from a string

前端 未结 7 1982
轻奢々
轻奢々 2020-12-08 17:02

I have a string in the form of:

s = \'A - 13, B - 14, C - 29, M - 99\'

and so on (the length varies). What is the easiest way to create a d

7条回答
  •  一个人的身影
    2020-12-08 17:17

    >>> dict((k.strip(),int(v.strip())) for k,v in (p.split('-') for p in s.split(',')))
    {'A': 13, 'B': 14, 'M': 99, 'C': 29}
    

提交回复
热议问题