Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #2698

Re: a better way to invert a list?

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.news-service.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; '>>>>': 0.09; 'none:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'solution,': 0.09; 'def': 0.13; 'wrote:': 0.14; 'inverse': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'valueerror': 0.16; 'seems': 0.21; 'subject:list': 0.22; 'function': 0.27; "doesn't": 0.28; 'thanks': 0.29; 'raise': 0.29; 'problem': 0.29; 'subject:?': 0.29; 'from:addr:web.de': 0.31; 'to:addr:python-list': 0.32; "isn't": 0.34; 'header:X-Complaints- To:1': 0.34; 'describe': 0.35; 'occurs': 0.35; 'received:org': 0.38; 'to:addr:python.org': 0.39; 'header:Mime-Version:1': 0.39; 'header:Received:5': 0.40; 'twice': 0.60; 'best': 0.60; 'subject:better': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: a better way to invert a list?
Date Wed, 06 Apr 2011 11:58:35 +0200
Organization None
References <2215eefd-3677-4459-8656-aa04978f6f3f@g7g2000pro.googlegroups.com> <mailman.61.1302039998.9059.python-list@python.org> <ad5aa280-8a34-44db-8e0e-9c1f382a4b2f@a17g2000yqn.googlegroups.com> <a265eb9c-eb57-416d-a71a-1ed755de8901@l30g2000vbn.googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084ba0e.dip.t-dialin.net
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.73.1302083939.9059.python-list@python.org> (permalink)
Lines 27
NNTP-Posting-Host 82.94.164.166
X-Trace 1302083939 news.xs4all.nl 41102 [::ffff:82.94.164.166]:41866
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:2698

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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