Re: flattening a dict

2008-02-21 Thread Benjamin
On Feb 21, 9:13 pm, George Sakkis <[EMAIL PROTECTED]> wrote: > On Feb 21, 8:04 pm, Benjamin <[EMAIL PROTECTED]> wrote: > > > > > On Feb 17, 6:18 am, Terry Jones <[EMAIL PROTECTED]> wrote: > > > > Hi Arnaud & Benjamin > > > > Here's a version that's a bit more general. It handles keys whose values >

Re: flattening a dict

2008-02-21 Thread George Sakkis
On Feb 21, 8:04 pm, Benjamin <[EMAIL PROTECTED]> wrote: > On Feb 17, 6:18 am, Terry Jones <[EMAIL PROTECTED]> wrote: > > > Hi Arnaud & Benjamin > > > Here's a version that's a bit more general. It handles keys whose values > > are empty dicts (assigning None to the value in the result), and also di

Re: flattening a dict

2008-02-21 Thread Benjamin
On Feb 17, 6:18 am, Terry Jones <[EMAIL PROTECTED]> wrote: > Hi Arnaud & Benjamin > > Here's a version that's a bit more general. It handles keys whose values > are empty dicts (assigning None to the value in the result), and also dict > keys that are not strings (see the test data below). It's als

Re: flattening a dict

2008-02-19 Thread Boris Borcic
Duncan Booth wrote: > In this particular case I think the lambda does contribute to the > obfuscation. Yes, they are single expressions, but only because that > have been contorted to become single expressions. The first two return > generators, so if you don't force them into a lambda you can wr

Re: flattening a dict

2008-02-19 Thread Duncan Booth
Steven D'Aprano <[EMAIL PROTECTED]> wrote: > # Untested > 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) els

Re: flattening a dict

2008-02-18 Thread Jeff Schwab
Arnaud Delobelle wrote: > On Feb 18, 10:22 pm, Steven D'Aprano <[EMAIL PROTECTED] > cybersource.com.au> wrote: > [...] >> 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 differe

Re: flattening a dict

2008-02-18 Thread Arnaud Delobelle
On Feb 18, 10:22 pm, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: [...] > 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 mult

Re: flattening a dict

2008-02-18 Thread Steven D'Aprano
On Mon, 18 Feb 2008 14:03:20 +, Duncan Booth wrote: > Why, why, why, why are you using lambda here? It only makes the code > harder to read (and it is bad enough without that). A lambda which is > assigned directly to a variable is a bad code smell. Oh come on. I don't get this allergy to lam

Re: flattening a dict

2008-02-18 Thread Boris Borcic
Duncan Booth wrote: > Boris Borcic <[EMAIL PROTECTED]> wrote: > >> 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

Re: flattening a dict

2008-02-18 Thread Duncan Booth
Boris Borcic <[EMAIL PROTECTED]> wrote: > 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(

Re: flattening a dict

2008-02-18 Thread Boris Borcic
Arnaud Delobelle wrote: > On Feb 17, 4:03 pm, Boris Borcic <[EMAIL PROTECTED]> wrote: >> George Sakkis wrote: >>> On Feb 17, 7:51 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: BTW, I keep using the idiom itertools.chain(*iterable). I guess that during function calls *iterable gets expa

Re: flattening a dict

2008-02-17 Thread Arnaud Delobelle
On Feb 17, 4:03 pm, Boris Borcic <[EMAIL PROTECTED]> wrote: > George Sakkis wrote: > > On Feb 17, 7:51 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > > >> BTW, I keep using the idiom itertools.chain(*iterable).  I guess that > >> during function calls *iterable gets expanded to a tuple.  Wouldn'

Re: flattening a dict

2008-02-17 Thread Terry Jones
> "Arnaud" == Arnaud Delobelle <[EMAIL PROTECTED]> writes: Arnaud> BTW, I keep using the idiom itertools.chain(*iterable). I guess Arnaud> that during function calls *iterable gets expanded to a tuple. Arnaud> Wouldn't it be nice to have an equivalent one-argument function Arnaud> that takes

Re: flattening a dict

2008-02-17 Thread Boris Borcic
George Sakkis wrote: > On Feb 17, 7:51 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > >> 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 ta

Re: flattening a dict

2008-02-17 Thread George Sakkis
On Feb 17, 7:51 am, Arnaud Delobelle <[EMAIL PROTECTED]> wrote: > 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 iterabl

Re: flattening a dict

2008-02-17 Thread Arnaud Delobelle
On Feb 17, 12:18 pm, Terry Jones <[EMAIL PROTECTED]> wrote: > Hi Arnaud & Benjamin > > Here's a version that's a bit more general. It handles keys whose values > are empty dicts (assigning None to the value in the result), and also dict > keys that are not strings (see the test data below). It's al

Re: flattening a dict

2008-02-17 Thread Terry Jones
Hi Arnaud & Benjamin Here's a version that's a bit more general. It handles keys whose values are empty dicts (assigning None to the value in the result), and also dict keys that are not strings (see the test data below). It's also less recursive as it only calls itself on values that are dicts.

Re: flattening a dict

2008-02-17 Thread Boris Borcic
Arnaud Delobelle wrote: > > In Python you can do anything, even ...pass the Turing test with a one-liner. Back after 9/11, when US patriotism was the rage, Python knew how to answer correctly the query filter(lambda W : W not in 'ILLITERATE','BULLSHIT') And Python 3.0 slated for next August o

Re: flattening a dict

2008-02-17 Thread Arnaud Delobelle
On Feb 17, 3:56 am, Benjamin <[EMAIL PROTECTED]> 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 t

Re: flattening a dict

2008-02-16 Thread Jeff Schwab
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