473,385 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

flattening a dict

How would I go about "flattening" a dict with many nested dicts
within? The dicts might look like this:
{"mays" : {"eggs" : "spam"},
"jam" : {"soda" : {"love" : "dump"}},
"lamba" : 23
}
I'd like it to put "/" inbetween the dicts to make it a one
dimensional dict and look like this:
{"mays/eggs" : "spam",
"jam/soda/love" : "dump",
"lamba" : 23
}
Feb 17 '08 #1
7 4812
Benjamin wrote:
How would I go about "flattening" a dict with many nested dicts
within? The dicts might look like this:
{"mays" : {"eggs" : "spam"},
"jam" : {"soda" : {"love" : "dump"}},
"lamba" : 23
}
I'd like it to put "/" inbetween the dicts to make it a one
dimensional dict and look like this:
{"mays/eggs" : "spam",
"jam/soda/love" : "dump",
"lamba" : 23
}
What have you tried so far?
Feb 17 '08 #2
On Feb 17, 3:56*am, Benjamin <musiccomposit...@gmail.comwrote:
How would I go about "flattening" a dict with many nested dicts
within? The dicts might look like this:
{"mays" : {"eggs" : "spam"},
"jam" : {"soda" : {"love" : "dump"}},
"lamba" : 23}

I'd like it to put "/" inbetween the dicts to make it a one
dimensional dict and look like this:
{"mays/eggs" : "spam",
"jam/soda/love" : "dump",
"lamba" : 23

}
In Python you can do anything, even flatten a dictionary:

from itertools import chain

def flattendict(d, pfx='', sep='/'):
if isinstance(d, dict):
if pfx: pfx += sep
return chain(*(flattendict(v, pfx+k, sep) for k, v in
d.iteritems()))
else:
return (pfx, d),

test = {"mays" : {"eggs" : "spam"},
"jam" : {"soda" : {"love" : "dump"}},
"lamba" : 23
}
>>print dict(flattendict(test))
{'lamba': 23, 'mays/eggs': 'spam', 'jam/soda/love': 'dump'}

You an even have other separators ;)
>>print dict(flattendict(test, sep='.'))
{'lamba': 23, 'jam.soda.love': 'dump', 'mays.eggs': 'spam'}

--
Arnaud

Feb 17 '08 #3
George Sakkis wrote:
On Feb 17, 7:51 am, Arnaud Delobelle <arno...@googlemail.comwrote:
>BTW, I keep using the idiom itertools.chain(*iterable). I guess that
during function calls *iterable gets expanded to a tuple. Wouldn't it
be nice to have an equivalent one-argument function that takes an
iterable of iterables and return the 'flattened' iterable?

Indeed; I don't have any exact numbers but I roughly use this idiom as
often or more as the case where chain() takes a known fixed number of
arguments. The equivalent function you describe is trivial:

def chain2(iter_of_iters):
for iterable in iter_of_iters:
for i in iterable:
yield i
or fwiw

chainstar = lambda iters : (x for it in iters for x in it)

- a form that better suggests how to inline it in the calling expression, if
applicable.

Cheers, BB

Feb 17 '08 #4
On Feb 17, 4:03*pm, Boris Borcic <bbor...@gmail.comwrote:
George Sakkis wrote:
On Feb 17, 7:51 am, Arnaud Delobelle <arno...@googlemail.comwrote:
BTW, I keep using the idiom itertools.chain(*iterable). *I guess that
during function calls *iterable gets expanded to a tuple. *Wouldn't it
be nice to have an equivalent one-argument function that takes an
iterable of iterables and return the 'flattened' iterable?
Indeed; I don't have any exact numbers but I roughly use this idiom as
often or more as the case where chain() takes a known fixed number of
arguments. The equivalent function you describe is trivial:
def chain2(iter_of_iters):
* for iterable in iter_of_iters:
* * *for i in iterable:
* * * * yield i

or fwiw

chainstar = lambda iters : (x for it in iters for x in it)

- a form that better suggests how to inline it in the calling expression, if
applicable.
Indeed:

def flattendict(d):
def gen(d, pfx=()):
return (x for k, v in d.iteritems()
for x in (gen(v, pfx+(k,)) if isinstance(v, dict)
else ((pfx+(k,), v),)))
return dict(gen(d))

I don't know, I find the chain(*...) version more readable, although
this one is probably better. Please, Mr itertools, can we have
chainstar?

--
Arnaud

Feb 17 '08 #5
Duncan Booth wrote:
Boris Borcic <bb*****@gmail.comwrote:
>It is more elementary in the mathematician's sense, and therefore
preferable all other things being equal, imo. I've tried to split
'gen' but I can't say the result is so much better.

def flattendict(d) :
gen = lambda L : (x for M in exp(L) for x in rec(M))
exp = lambda L : (L+list(kv) for kv in L.pop().iteritems())
rec = lambda M : gen(M) if isinstance(M[-1],dict) else [M]
return dict((tuple(L[:-1]),L[-1]) for L in gen([d]))

Why, why, why, why are you using lambda here?
Because the 3 lambdas make manifest that they could be combined into a single
expression and thus reveal that they replace a single expression.
It only makes the code harder
to read
Matter of perceptions. I myself tend to find

def g(...) :
def f(x) :
return (whatever)
return y(f)

a bit hard to follow. So frankly I don't feel it betters anything to write

def flattendict(d) :
def gen(L) :
return (x for M in exp(L) for x in rec(M))

def exp(L) :
return (L+list(kv) for kv in L.pop().iteritems())

def rec(M) :
return gen(M) if isinstance(M[-1],dict) else [M]

return dict((tuple(L[:-1]),L[-1]) for L in gen([d]))

But whatever to please you

Cheers, BB
Feb 18 '08 #6
On Feb 18, 10:22*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
[...]
The problem with lambdas comes from people trying to hammer multi-
expression functions into a single-expression lambda, hence obfuscating
the algorithm. That's no different from people who obfuscate multi-
expression functions by writing them as a generator expression.
I'm sorry if my Python is difficult to understand. That's because
I've got a bit of a Lisp...

--
Arnaud

Feb 18 '08 #7
Arnaud Delobelle wrote:
On Feb 18, 10:22 pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.auwrote:
[...]
>The problem with lambdas comes from people trying to hammer multi-
expression functions into a single-expression lambda, hence obfuscating
the algorithm. That's no different from people who obfuscate multi-
expression functions by writing them as a generator expression.

I'm sorry if my Python is difficult to understand. That's because
I've got a bit of a Lisp...
That may be the first lambda-related humor I've ever heard.
Feb 18 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Robin Cull | last post by:
Imagine I have a dict looking something like this: myDict = {"key 1": , "key 2": , "key 3": , "key 4": } That is, a set of keys which have a variable length list of associated values after...
1
by: solex | last post by:
Hello, Part of my XML application will allow the user to select any field(s) from the hiearchy as output to a CSV file for viewing/ manipulation. The datastore consists of many XML files that...
3
by: Bengt Richter | last post by:
Has anyone found a way besides not deriving from dict? Shouldn't there be a way? TIA (need this for what I hope is an improvement on the Larosa/Foord OrderedDict ;-) I guess I can just document...
11
by: sandravandale | last post by:
I can think of several messy ways of making a dict that sets a flag if it's been altered, but I have a hunch that experienced python programmers would probably have an easier (well maybe more...
15
by: Cruella DeVille | last post by:
I'm trying to implement a bookmark-url program, which accepts user input and puts the strings in a dictionary. Somehow I'm not able to iterate myDictionary of type Dict{} When I write print...
2
by: gangesmaster | last post by:
as we all know, * (asterisk) can be used to "inline" or "flatten" a tuple into an argument list, i.e.: def f(a, b, c): ... x = (1,2,3) f(*x) so... mainly for symmetry's sake, why not make a...
15
by: George Sakkis | last post by:
Although I consider dict(**kwds) as one of the few unfortunate design choices in python since it prevents the future addition of useful keyword arguments (e.g a default value or an orderby...
12
by: jeremito | last post by:
Please excuse me if this is obvious to others, but I can't figure it out. I am subclassing dict, but want to prevent direct changing of some key/value pairs. For this I thought I should override...
0
by: wilko | last post by:
Hi, Does anybody have any design ideas for flattening multiple measurement records into fact table columns. Our current design imports the measurement records into a staging table and then uses...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.