Re: Python one-liner?

2012-04-16 Thread Ian Kelly
On Mon, Apr 16, 2012 at 3:18 PM, Karl Knechtel wrote: > d = {k: list(v) for k, v in itertools.groupby(sorted(l, key=f), f)} Note that the sorted call would fail if f returns objects of unorderable types: Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32 Type "hel

Fwd: Python one-liner?

2012-04-16 Thread Karl Knechtel
On Sat, Apr 14, 2012 at 7:26 AM, Tim Chase wrote: > > On 04/13/12 22:54, Chris Angelico wrote: >> >> Yes, that would be the right method to use. I'd not bother with the >> function and map() though, and simply iterate: >> >> d = {} >> for val in l: >>  d.setdefault(f(val), []).append(val) > > > Or

Re: Python one-liner?

2012-04-14 Thread Tim Chase
On 04/13/12 22:54, Chris Angelico wrote: Yes, that would be the right method to use. I'd not bother with the function and map() though, and simply iterate: d = {} for val in l: d.setdefault(f(val), []).append(val) Or make d a defaultdict: from collections import defaultdict d = defaultd

Re: Python one-liner?

2012-04-13 Thread Chris Angelico
On Sat, Apr 14, 2012 at 1:44 PM, Evan Driscoll wrote: > Ha ha, sorry I can't read right now apparently. dict.setdefault does > exactly what I wanted. > > (The name just prompted another interpretation in my mind which doesn't > work and I got tunnel vision. I'll stop spamming now, and we return to

Re: Python one-liner?

2012-04-13 Thread Evan Driscoll
On 4/13/2012 22:42, Evan Driscoll wrote: > Though I might as well ask another question... if I have a dict with > values which are lists, what's a good way to say "append x to the list > at key k, creating a list if it's not there"? dict.setdefault seems > potentially promising but the docs are cra

Re: Python one-liner?

2012-04-13 Thread Evan Driscoll
On 4/13/2012 22:33, Evan Driscoll wrote: > d = {} > def appender(e): > d.get(f(e), []).append(e) > map(appender, l) Just in case it isn't clear, the above has at least two problems and won't even come close to working. :-) Though I might as well ask another question... if I h

Python one-liner?

2012-04-13 Thread Evan Driscoll
I have a function 'f' and a list 'l'. I want a dictionary where the keys are evaluations of 'f(thing from l)' and the values are lists of stuff from 'l' that matches. So for instance, if 'f = lambda x: x%3' and 'l=range(9)', then I want { 0: [0,3,6], 1:[1,4,7], 2:[2,5,8]}. I can do that with an ex

Re: Python one-liner??

2008-08-22 Thread Paddy
On Aug 22, 10:03 pm, Matimus <[EMAIL PROTECTED]> wrote: > > Do we have python one-liner like perl one-liner 'perl -e'?? > > The answer is python -c... > > but python -h is useful too. > > Matt And Python is not optimised for one-liner solutions. I ha

Re: Python one-liner??

2008-08-22 Thread James Matthews
What i use them for is to test for packages. e.g python -c "import django" On Fri, Aug 22, 2008 at 2:03 PM, Matimus <[EMAIL PROTECTED]> wrote: > > Do we have python one-liner like perl one-liner 'perl -e'?? > > > The answer is python -c... > > bu

Re: Python one-liner??

2008-08-22 Thread Matimus
> Do we have python one-liner like perl one-liner 'perl -e'?? The answer is python -c... but python -h is useful too. Matt -- http://mail.python.org/mailman/listinfo/python-list

Re: Python one-liner??

2008-08-22 Thread Dan
On Fri, Aug 22, 2008 at 4:13 PM, Wojtek Walczak <[EMAIL PROTECTED]> wrote: > -c? > -- > http://mail.python.org/mailman/listinfo/python-list > Yup. Stands for command python -c "print 'Hello World!'" Hello World! -- http://mail.python.org/mailman/listinfo/python-list

Re: Python one-liner??

2008-08-22 Thread Wojtek Walczak
-c? -- http://mail.python.org/mailman/listinfo/python-list

Python one-liner??

2008-08-22 Thread srinivasan srinivas
Hi, Do we have python one-liner like perl one-liner 'perl -e'?? Thanks, Srini Unlimited freedom, unlimited storage. Get it now, on http://help.yahoo.com/l/in/yahoo/mail/yahoomail/tools/tools-08.html/ -- http://mail.python.org/mailman/listinfo/python-list