How convert list to nested dictionary?

M

macm

Hi Chris

Thanks for your hint.

I am reading this

http://www.amk.ca/python/writing/functional

Do you have good links or books to me learn "Functional Programming"?

but I am not asking "...because is easy but because is hard."

Show me, please! if you can.

Thanks is advance.

Best regards

macm


How convert list to nested dictionary?
l ['k1', 'k2', 'k3', 'k4', 'k5']
result
{'k1': {'k2': {'k3': {'k4': {'k5': {}}}}}}

We don't do homework.
Hint: Iterate through the list in reverse order, building up your
result. Using reduce() is one option.

Cheers,
Chris
 
C

Chris Rebert

How convert list to nested dictionary?
l
['k1', 'k2', 'k3', 'k4', 'k5']
result
{'k1': {'k2': {'k3': {'k4': {'k5': {}}}}}}

We don't do homework.
Hint: Iterate through the list in reverse order, building up your
result. Using reduce() is one option.

Thanks for your hint.
Do you have good links or books to me learn "Functional Programming"?

Relevant to the particular problem you posed:
http://en.wikipedia.org/wiki/Fold_(higher-order_function)

Show me, please! if you can.

I will give you this further hint:

def reducer(accumulator, elem):
# if accumulator = {'k5': {} }
# and elem = 'k4'
# then we want to return {'k4': {'k5': {} } }
now_implement_me()

l = ['k1', 'k2', 'k3', 'k4', 'k5']
result = reduce(reducer, reversed(l), {})

Note that:
reduce(reducer, reversed(l), {})
Is basically equivalent to:
reducer( reducer( reducer( reducer( reducer({}, 'k5'), 'k4'), 'k3'),
'k2'), 'k1')

Cheers,
Chris
 
P

Peter Otten

Boris said:
Arnaud said:
macm said:
Hi Folks

How convert list to nested dictionary?

l
['k1', 'k2', 'k3', 'k4', 'k5']
result
{'k1': {'k2': {'k3': {'k4': {'k5': {}}}}}}

Regards

macm

reduce(lambda x,y: {y:x}, reversed(l), {})

d={}
while L : d={L.pop():d}

Iterating over the keys in normal order:
.... outer[key] = inner = {}
.... outer = inner
....{'a': {'b': {'c': {'d': {'e': {}}}}}}

The "functional" variant:
{'a': {'b': {'c': {'d': {'e': {}}}}}}

In a single expression if you are willing to pay the price:
"abcde", ({},)*2)[0]
{'a': {'b': {'c': {'d': {'e': {}}}}}}

Peter
 
M

macm

Hi Folks

thanks a lot all. All solutions work fine.

while I am doing my home work.
Reading "Learning Python" and much more.

Let me ask again to close my doubts:
l = ['k1', 'k2', 'k3', 'k4', 'k5']
d = reduce(lambda x,y: {y:x}, reversed(l), {'/':[1,2,3]})
d {'k1': {'k2': {'k3': {'k4': {'k5': {'/': [1, 2, 3]}}}}}}
d['k1']['k2']['k3']['k4']['k5'] {'/': [1, 2, 3]}
d['k1']['k2']['k3']['k4']['k5']['/'] [1, 2, 3]

now I want generate the "index" to access the element.

==> d['k1']['k2']['k3']['k4']['k5']['/'] from l

So again I have only.
l = ['k1', 'k2', 'k3', 'k4', 'k5']

z = ?magicCode?

z = d['k1']['k2']['k3']['k4']['k5']['/']
[1, 2, 3, 4]

Best Regards

macm



Boris said:
Arnaud said:
Hi Folks
How convert list to nested dictionary?
l
['k1', 'k2', 'k3', 'k4', 'k5']
result
{'k1': {'k2': {'k3': {'k4': {'k5': {}}}}}}
Regards
macm
reduce(lambda x,y: {y:x}, reversed(l), {})
d={}
while L : d={L.pop():d}

Iterating over the keys in normal order:

...     outer[key] = inner = {}
...     outer = inner
...>>> d

{'a': {'b': {'c': {'d': {'e': {}}}}}}

The "functional" variant:

{'a': {'b': {'c': {'d': {'e': {}}}}}}

In a single expression if you are willing to pay the price:

"abcde", ({},)*2)[0]
{'a': {'b': {'c': {'d': {'e': {}}}}}}

Peter
 
P

Peter Otten

macm said:
thanks a lot all. All solutions work fine.

while I am doing my home work.
Reading "Learning Python" and much more.

Let me ask again to close my doubts:
l = ['k1', 'k2', 'k3', 'k4', 'k5']
d = reduce(lambda x,y: {y:x}, reversed(l), {'/':[1,2,3]})
d {'k1': {'k2': {'k3': {'k4': {'k5': {'/': [1, 2, 3]}}}}}}
d['k1']['k2']['k3']['k4']['k5'] {'/': [1, 2, 3]}
d['k1']['k2']['k3']['k4']['k5']['/'] [1, 2, 3]

now I want generate the "index" to access the element.

==> d['k1']['k2']['k3']['k4']['k5']['/'] from l

So again I have only.
l = ['k1', 'k2', 'k3', 'k4', 'k5']

z = ?magicCode?

z = d['k1']['k2']['k3']['k4']['k5']['/']

You'll eventually have to start and write your first line of code. Why not
doing it right now? It is sure more rewarding than copying other people's
canned solutions and it can even be fun.

Peter
 
M

macm

Hi Peter

Thanks a lot for your tips and codes,

Cake Recipes are good to learn! So I post just basic issues.

Hopping a good soul like you can help me!

But I am still learning... : )

Best Regards

macm

macm said:
thanks a lot all. All solutions work fine.
while I am doing my home work.
Reading "Learning Python" and much more.
Let me ask again to close my doubts:
l = ['k1', 'k2', 'k3', 'k4', 'k5']
d = reduce(lambda x,y: {y:x}, reversed(l), {'/':[1,2,3]})
d
{'k1': {'k2': {'k3': {'k4': {'k5': {'/': [1, 2, 3]}}}}}}
d['k1']['k2']['k3']['k4']['k5'] {'/': [1, 2, 3]}
d['k1']['k2']['k3']['k4']['k5']['/']
[1, 2, 3]
now I want generate the "index" to access the element.
==> d['k1']['k2']['k3']['k4']['k5']['/'] from l
So again I have only.
l = ['k1', 'k2', 'k3', 'k4', 'k5']
z = ?magicCode?
z = d['k1']['k2']['k3']['k4']['k5']['/']

You'll eventually have to start and write your first line of code. Why not
doing it right now? It is sure more rewarding than copying other people's
canned solutions and it can even be fun.

Peter
 
M

macm

Ok. Done

.... nest2 = nest
.... for key in path[:-1]:
.... nest2 = nest2[key]
.... nest2[path[-1]].append(val)
.... return nest
....
l = ['k1','k2','k3','k4','k5']
ndict = reduce(lambda x,y: {y:x}, reversed(l), {'/':[1,2,3]})
x = l + list('/')
print appendNested(ndict, x, 5) {'k1': {'k2': {'k3': {'k4': {'k5': {'/': [1, 2, 3, 5]}}}}}}


I need make lambda but now is friday night here and I guess I will go
drink a beer or three!

Cheers!

macm


Hi Peter

Thanks a lot for your tips and codes,

Cake Recipes are good to learn! So I post just basic issues.

Hopping a good soul like you can help me!

But I am still learning... : )

Best Regards

macm

macm said:
thanks a lot all. All solutions work fine.
while I am doing my home work.
Reading "Learning Python" and much more.
Let me ask again to close my doubts:
l = ['k1', 'k2', 'k3', 'k4', 'k5']
d = reduce(lambda x,y: {y:x}, reversed(l), {'/':[1,2,3]})
d
{'k1': {'k2': {'k3': {'k4': {'k5': {'/': [1, 2, 3]}}}}}}
d['k1']['k2']['k3']['k4']['k5']
{'/': [1, 2, 3]}
d['k1']['k2']['k3']['k4']['k5']['/']
[1, 2, 3]
now I want generate the "index" to access the element.
==> d['k1']['k2']['k3']['k4']['k5']['/'] from l
So again I have only.
l = ['k1', 'k2', 'k3', 'k4', 'k5']
z = ?magicCode?
z = d['k1']['k2']['k3']['k4']['k5']['/']
You'll eventually have to start and write your first line of code. Why not
doing it right now? It is sure more rewarding than copying other people's
canned solutions and it can even be fun.
 
A

Arnaud Delobelle

Peter Otten said:
Boris said:
Arnaud said:
Hi Folks

How convert list to nested dictionary?

l
['k1', 'k2', 'k3', 'k4', 'k5']
result
{'k1': {'k2': {'k3': {'k4': {'k5': {}}}}}}

Regards

macm

reduce(lambda x,y: {y:x}, reversed(l), {})

d={}
while L : d={L.pop():d}

Iterating over the keys in normal order:
... outer[key] = inner = {}
... outer = inner
...{'a': {'b': {'c': {'d': {'e': {}}}}}}

The "functional" variant:
{'a': {'b': {'c': {'d': {'e': {}}}}}}

In a single expression if you are willing to pay the price:
"abcde", ({},)*2)[0]
{'a': {'b': {'c': {'d': {'e': {}}}}}}

Peter

The most basic functional version:
f = lambda l: {l[0]: f(l[1:])} if l else {}
f('abcde')
{'a': {'b': {'c': {'d': {'e': {}}}}}}
 
A

Alexander Gattin

Hello,

How convert list to nested dictionary?
l
['k1', 'k2', 'k3', 'k4', 'k5']
result
{'k1': {'k2': {'k3': {'k4': {'k5': {}}}}}}

http://www.amk.ca/python/writing/functional

so, why didn't you try python's reduce?

IMO this is trivial:
l ['k1', 'k2', 'k3', 'k4', 'k5']
reduce(lambda x,y: {y:x}, l.__reversed__()) {'k1': {'k2': {'k3': {'k4': 'k5'}}}}
reduce(lambda x,y: {y:x}, l.__reversed__(), dict()) {'k1': {'k2': {'k3': {'k4': {'k5': {}}}}}}

second reduce uses empty dict for seed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,792
Messages
2,569,639
Members
45,353
Latest member
RogerDoger

Latest Threads

Top