[Python-Dev] decorate-sort-undecorate
Peter Norvig
pnorvig at google.com
Wed Oct 15 20:27:40 EDT 2003
Greg Ewing wrote:
> [x >> key(x) for x in l] # ascending sort
> [x << key(x) for x in l] # descending sort
>
>(Well, we got print >> f, so it was worth a try...)
I hope you're not serious about that.
As it turns out, I have a proposed syntax for something I call an
"accumulation display", and with it I was able to implement and test a
SortBy in about a minute. It uses the syntax
>>> [SortBy: abs(x) for x in (-2, -4, 3, 1)]
[1, -2, 3, -4]
where SortBy is an expression (in this case an identifier bound to a
class object), not a keyword. Other examples of accumulation displays
include:
[Sum: x*x for x in numbers]
[Product: Prob_spam(word) for word in email_msg]
[Min: temp(hour) for hour in range(24)]
[Top(10): humor(joke) for joke in jokes]
[Argmax: votes[c] for c in candidates]
You can read the whole proposal at http:///www.norvig.com/pyacc.html
-Peter Norvig
More information about the Python-Dev
mailing list