So I am trying to format a string to a dict. Meaning, I receive an str input of this sort:
"b:0.1 a:0.5 n:0.2 p:0.1 c:0.1 k:0.1"
And wish to convert it to dict:
{'b':0.1,'a':0.5,'n':0.2 and so on... 'k':0.1}
So far I did the exact opposite:
def string_to_ngram_dict(x):
y = {"{!s}:{!r}".format(key, val) for (key, val) in x.items()}
input:
{ “b”: 0.1, “a”: 0.4, “n”: 0.2, “p”: 0.1, “c”: 0.1, “k”: 0.1 }
output:
"b:0.1 a:0.5 n:0.2 p:0.1 c:0.1 k:0.1”
So I was wondering if there is any cool trick to reverse this function I wrote. If not, any ideas on how to to do it?
Thanks