list -> dict help

Bill Scherer scherbi at bam.com
Mon Jan 31 14:12:46 EST 2000


Thanks! This works almost perfect.  My only problem now is that of course I'm
dealing with many lines of input, and I want to put them all in the same
dictionary. (sorry for not mentioning this originally).  eg:

I'll have many lists that are functionally equivalent to the following:

['a', 'b', 'c', 1]
['a', 'b', 'r', 21]
['a', 'g', 'e', 'z', 13]
etc...

my earlier statement, that l[:-2] are netsed keys and l[-2:] is the terminal
key/value pair, still holds.

My first inclination was to use the dictionary update method, but that
overwrites existing keys.  I need something to add dictionaries together.  I'll
hack this together on my own unless someone has somthing ready to go...

Thanks again.

Bill

Remco Gerlich wrote:

> Remco Gerlich wrote in comp.lang.python:
> > def convert(l):
> >     if len(l) == 1:
> >          return l[0]
> >       else:
> >          return { l[0] : convert(l[1:]) }
> >
> > But this copies the list every call, and recursion can use a lot of stack.
> > Since it's tail recursion, there must be an iterative version.
>
> Actually, this being Python, it's all done by reference and the lists
> aren't copied all of the time. My brain is still thinking in terms of
> other languages now and then...
>
> That means the recursive solution is quite good already, I think.
> Can't benchmark them at the moment though.
>
> Also, my editor treats .py files and Usenet posts differently space/tab-wise,
> and copying the functions from my testfile mangled the indentation :(.
>
> --
> Remco Gerlich,  scarblac at pino.selwerd.nl
> Hi! I'm a .sig virus! Join the fun and copy me into yours!
> --
> http://www.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list