a better way to invert a list?

Paul Rubin no.email at nospam.invalid
Wed Apr 6 15:51:32 EDT 2011


scattered <tooscattered at 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).


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /