convert string to dict using list comprehension

情到浓时终转凉″ 提交于 2019-12-03 01:59:46
S.Lott

Do you mean this?

>>> dict( (n,int(v)) for n,v in (a.split('=') for a in string.split() ) )
{'a': 0, 'c': 3, 'b': 1}

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}

Try the next:

dict([x.split('=') for x in s.split()])

I sometimes like this approach, especially when the logic for making keys and values is more complicated:

s = "a=0 b=1 c=3"

def get_key_val(x):
    a,b = x.split('=')
    return a,int(b)

ans = dict(map(get_key_val,s.split()))

Nowadays you should probably use a dictionary comprehension, introduced in 2.7:

mydict = {key: int(value) for key, value in (a.split('=') for a in mystring.split())}

The dictionary comprehension is faster and shinier (and, in my opinion, more readable).

from timeit import timeit

setup = """mystring = "a=0 b=1 c=3\""""
code1 = """mydict = dict((n,int(v)) for n,v in (a.split('=') for a in mystring.split()))""" # S.Lott's code
code2 = """mydict = {key: int(value) for key, value in (a.split('=') for a in mystring.split())}"""

print timeit(code1, setup=setup, number=10000) # 0.115524053574
print timeit(code2, setup=setup, number=10000) # 0.105328798294
from cgi import parse_qsl
text = "a=0 b=1 c=3"
dic = dict((k, int(v)) for k, v in parse_qsl(text.replace(' ', '&')))
print dic

prints

{'a': 0, 'c': 3, 'b': 1}

I would do this:

def kv(e): return (e[0], int(e[1]))
d = dict([kv(e.split("=")) for e in string.split(" ")])

I like S.Lott's solution, but I came up with another possibility.
Since you already have a string resembling somehow the way you'd write that, you can just adapt it to python syntax and then eval() it :)

import re
string = "a=0 b=1 c=3"
string2 = "{"+ re.sub('( |^)(?P<id>\w+)=(?P<val>\d+)', ' "\g<id>":\g<val>,', string) +"}"
dict = eval(string2)
print type(string), type(string2), type(dict)
print string, string2, dict

The regex here is pretty raw and won't catch all the possible python identifiers, but I wanted to keep it simple for simplicity's sake. Of course if you have control over how the input string is generated, just generate it according to python syntax and eval it away. BUT of course you should perform additional sanity checks to make sure that no code is injected there!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!