string to dictionary
Sean 'Shaleh' Perry
shalehperry at attbi.com
Fri Mar 1 19:35:35 EST 2002
On 01-Mar-2002 les ander wrote:
> Hi,
> i have a list of strings like this:
>
> aList=[ 'a_1 b_1', 'a_2 b_2', 'a_3 b_3',...]
>
> i want to convert this to a dictionary with a_i -> b_i without
> using loops (it is trivial to do it with loops)
> i tried this
>
> dict={}
> map(lambda x,d=dict: c=string.split(x), d[c[0]]=c[1], aList)
>
> but it complains that "keyword can't be an expression"
> can someone please help me get this right?
not sure if there is a better way to do this, I just wanted to make what you
had work. Use of '=' is not allowed in lambdas.
d.setdefault(*string.split(x)) works for me here. Yes it is quite ugly. The
*foo syntax causes the tuple that split returns to be flattened into an
argument list. This also may not work on 1.5, do not have a copy here to test.
More information about the Python-list
mailing list