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
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
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
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
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
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
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
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
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
> 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
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
-c?
--
http://mail.python.org/mailman/listinfo/python-list
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
13 matches
Mail list logo