Glazner wrote: >> > def invert(p): >> > inverse = [None] * len(p) >> > for (i, j) in enumerate(p): >> > inverse[j] = i >> > return inverse >> >> Elegant. This seems like the best solution, although it isn't as much >> fun to write as a "one-liner". Thanks > > >>>> invert([1, 2, 3, 1]) > [None, 3, 1, 2] #blah
1 occurs twice in [1, 2, 3, 1] which therefore doesn't describe a permutation. In general a function has to be "bijective" to be invertable. You can catch the problem with (untested) def invert(p): inverse = [None] * len(p) for i, k in enumerate(p): if inverse[k] is not None: raise ValueError inverse[k] = i return inverse -- http://mail.python.org/mailman/listinfo/python-list