convert string to dict using list comprehension

后端 未结 8 1901
情歌与酒
情歌与酒 2021-02-05 12:34

I have came across this problem a few times and can\'t seem to figure out a simple solution. Say I have a string

string = \"a=0 b=1 c=3\"

I wan

8条回答
  •  抹茶落季
    2021-02-05 13:26

    How about a one-liner without list comprehension?

     foo="a=0 b=1 c=3"
     ans=eval( 'dict(%s)'%foo.replace(' ',',')) )
     print ans
    {'a': 0, 'c': 3, 'b': 1}
    

提交回复
热议问题