Hi,
I am trying to come up with a clean and simple way to sort a list of
objects (in this case SQLObject instances) by multiple attributes.
e.g. a Person object may have an age, a lastName, and a firstName.
I'd like to be able to arbitrarily sort by any combination of those in
any order.
I woul
For multiple keys the form is quite analogous:
L.sort(key=lambda i: (i.whatever, i.someother, i.anotherkey))
I.e., just return a tuple with the keys in order from your lambda.
Such tuples sort nicely.
-- George Young
--
http://mail.python.org/mailman/listinfo/python-list
If you are lambda-phobic (as I am) this should also work for an
arbitrary set of attributes:
attrs = 'attr1 attr2 attr3'.split()
sortlist = [[getattr(o,a) for a in attrs] + [o] for o in objects]
sorted_objects = [x[-1] for x in sorted(sortlist)]
-Noah
--
http://mail.python.org/mailman/listinfo/
gry@ll.mit.edu wrote:
> For multiple keys the form is quite analogous:
>
>L.sort(key=lambda i: (i.whatever, i.someother, i.anotherkey))
>
> I.e., just return a tuple with the keys in order from your lambda.
> Such tuples sort nicely.
In Python 2.5 you can do this with operator.attrgetter()
Kent Johnson wrote:
> In Python 2.5 you can do this with operator.attrgetter():
> L.sort(key=operator.attrgetter('whatever', 'someother', 'anotherkey'))
Note: this is also available in Python 2.4
--Scott David Daniels
[EMAIL PROTECTED]
--
http://mail.python.org/mailman/listinfo/python-list
Scott David Daniels wrote:
> Kent Johnson wrote:
>> In Python 2.5 you can do this with operator.attrgetter():
>> L.sort(key=operator.attrgetter('whatever', 'someother', 'anotherkey'))
>
> Note: this is also available in Python 2.4
No, the ability to specify more than one attribute name, making
[George Young]
>> For multiple keys the form is quite analogous:
>>
>> L.sort(key=lambda i: (i.whatever, i.someother, i.anotherkey))
[Noah]
> If you are lambda-phobic (as I am) this should also work for an
> arbitrary set of attributes:
>
> attrs = 'attr1 attr2 attr3'.split()
> sortlist = [[getat
Kent Johnson wrote:
> Scott David Daniels wrote:
>> Kent Johnson wrote:
>>> In Python 2.5 you can do this with operator.attrgetter():
>>> L.sort(key=operator.attrgetter('whatever', 'someother', 'anotherkey'))
>>
>> Note: this is also available in Python 2.4
>
> No, the ability to specify more th
I'm sure my avoidance of lambdas as due as much to laziness as
adherence to principle. This is a good opportunity to learn about them.
I suggested the above because it wasn't obvious to me how one would
pass the arbitrary set of attributes to the lambda expression (and I
envisioned them being spe
Raymond Hettinger wrote:
>
> The cult of lambda avoidance has lost contact with reality. [...]
> Lambda avoidance is rooted in two things, an aversion to the keyword
> name [...]
Let's push the diagnosis a bit further : the aversion to the keyword
"lambda" has to do with the fact that it ignore
Azolex:
> Let's push the diagnosis a bit further : the aversion to the keyword
> "lambda" has to do with the fact that it ignores the english word used
> by all non-geeks to convey the meaning, eg "given"
Right. However, Guido has said that lambda is here to stay,
so it's time to get over it.
R
[Noah]
> I suggested the above because it wasn't obvious to me how one would
> pass the arbitrary set of attributes to the lambda expression (and I
> envisioned them being specified as strings in this case, since the set
> of attributes will be coming from a web form).
>
> So what about the followi
Raymond Hettinger wrote:
> Azolex:
>> Let's push the diagnosis a bit further : the aversion to the keyword
>> "lambda" has to do with the fact that it ignores the english word used
>> by all non-geeks to convey the meaning, eg "given"
>
> Right. However, Guido has said that lambda is here to stay
13 matches
Mail list logo