Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #2718
| From | Paul Rubin <no.email@nospam.invalid> |
|---|---|
| Newsgroups | comp.lang.python |
| Subject | Re: a better way to invert a list? |
| References | <2215eefd-3677-4459-8656-aa04978f6f3f@g7g2000pro.googlegroups.com> |
| Date | 2011-04-06 12:51 -0700 |
| Message-ID | <7xwrj7kvjv.fsf@ruckus.brouhaha.com> (permalink) |
| Organization | Nightsong/Fort GNOX |
scattered <tooscattered@gmail.com> writes:
> def invert(p):
> return [ j for (i,j) in sorted(zip(p,range(len(p))))]
return [j for i,j in sorted(enumerate(p), key=itemgetter(1))]
looks a little cleaner to me.
In Haskell or ML, you can use patterns that contain wild
cards that play a role in the pattern-matching but don't establish any
binding. Can that be done in Python?
Not as much. You could say something like
sorted(enumerate(p), key=lambda(_,j): j)
which gets the meaning across (it binds the symbol "_" though this
doesn't escape the lambda).
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar
a better way to invert a list? scattered <tooscattered@gmail.com> - 2011-04-05 14:17 -0700
Re: a better way to invert a list? Ian Kelly <ian.g.kelly@gmail.com> - 2011-04-05 15:46 -0600
Re: a better way to invert a list? scattered <tooscattered@gmail.com> - 2011-04-05 16:24 -0700
Re: a better way to invert a list? Glazner <yoavglazner@gmail.com> - 2011-04-06 01:48 -0700
Re: a better way to invert a list? scattered <tooscattered@gmail.com> - 2011-04-06 02:48 -0700
Re: a better way to invert a list? Peter Otten <__peter__@web.de> - 2011-04-06 11:58 +0200
Re: a better way to invert a list? Raymond Hettinger <python@rcn.com> - 2011-04-05 17:07 -0700
Re: a better way to invert a list? Paul Rubin <no.email@nospam.invalid> - 2011-04-06 12:51 -0700
Re: a better way to invert a list? Ian Kelly <ian.g.kelly@gmail.com> - 2011-04-06 14:08 -0600
csiph-web