string to dictionary 
Author Message
 string to dictionary

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?
thanks
les



Wed, 18 Aug 2004 07:22:21 GMT  
 string to dictionary

Quote:

> 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)

try:
   >>> map(lambda x: d.__setitem__(*x), [x.split() for x in aList])
   [None, None, None]
   >>> d
   {'a_3': 'b_3', 'a_2': 'b_2', 'a_1': 'b_1'}

of with 2.2:
   >>> d = dict([x.split() for x in aList])
   >>> d
   {'a_3': 'b_3', 'a_2': 'b_2', 'a_1': 'b_1'}
--
groetjes, carel



Wed, 18 Aug 2004 08:31:42 GMT  
 string to dictionary


Quote:
> 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.



Wed, 18 Aug 2004 08:35:35 GMT  
 string to dictionary

Quote:

> 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)

map(lambda x,d=dict: c=string.split(x), d[c[0]]=c[1], aList)
                      ^                 ^    
First, assignment is not allowed within lambda.  Secondly, the scope of
lambda ends at the first comma (correct me if I were wrong).  Hence
d[c[0]=c[1] become a list and since c is not defined outside lambda, the
error message resulted.

The following code should work:

Quote:
>>> aList=[ 'a_1 b_1', 'a_2 b_2', 'a_3 b_3']
>>> dict = {}
>>> map( lambda x, d=dict: d.update( {x.split()[0] : x.split()[1]}), aList)
[None, None, None]
>>> dict

{'a_1': 'b_1', 'a_2': 'b_2', 'a_3': 'b_3'}

I would like to see a better solution as I find using split() twice ugly

Quote:

> but it complains that "keyword can't be an expression"
> can someone please help me get this right?
> thanks
> les

Bernie

--
There are three schools of magic.  One:  State a tautology, then ring
the changes on its corollaries; that's philosophy.  Two:  Record many
facts.  Try to find a pattern.  Then make a wrong guess at the next
fact; that's science.  Three:  Be aware that you live in a malevolent
Universe controlled by Murphy's Law, sometimes offset by Brewster's
Factor; that's engineering.  So far as I can remember, there is not one
word in the Gospels in praise of intelligence.

                -- Bertrand Russell



Wed, 18 Aug 2004 09:13:27 GMT  
 string to dictionary


Quote:
> 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)

This ought to do it:
Quote:
>>> aList=[ 'a_1 b_1', 'a_2 b_2', 'a_3 b_3']
>>> dict(map(lambda g: g.split() ,aList))

{'a_3': 'b_3', 'a_2': 'b_2', 'a_1': 'b_1'}

If you don't have python 2.2, then build the dictionary directly:

Quote:
>>> d = {}
>>> map( lambda (k,v): d.__setitem__(k,v), map(string.split, aList) )
[None, None, None]
>>> d

{'a_3': 'b_3', 'a_2': 'b_2', 'a_1': 'b_1'}

Note, the use of __setitem__ to avoid '='.
Still, it's better to get Python 2.2 and avoid the second
method.  Good functional style stays away from
lambdas with side-effects (setting the dictionary).

Quote:
> i tried this

> dict={}
> map(lambda x,d=dict: c=string.split(x), d[c[0]]=c[1], aList)

I recommend:
1. Never use an assignment inside a lambda expression
2. Never name a dictionary 'dict' which overrides the constructor
3. If lambdas get confusing, write a simple, working 'def' first
    and use that in the map.

Quote:
> but it complains that "keyword can't be an expression"
> can someone please help me get this right?
> thanks
> les

Good luck,

Raymond Hettinger



Wed, 18 Aug 2004 15:45:21 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Convert String to Dictionary question

2. Dictionary to string and back to dictionary??

3. Placing String Controls with Dictionary Fields

4. Integer and String Dictionary Keys for Fast Access

5. Get dictionary-keys used to format a string

6. string/dictionary substitution error

7. Strings --> Dictionaries

8. convert dictionary to string

9. string formatting with missing dictionary key

10. String formatting with two dictionaries?

11. Dictionary of dictionaries

12. losrt -dictionary and dictionary sorting...

 

 
Powered by phpBB® Forum Software