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


Groups > comp.lang.python > #75926

Re: how to get the ordinal number in list

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
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; 'indices': 0.07; '[1,': 0.09; 'iterate': 0.09; 'occurrences': 0.09; 'subject:number': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'cc:addr:python-list': 0.11; 'def': 0.12; 'argument:': 0.16; 'cleaner': 0.16; 'doing,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'loop.': 0.16; 'true:': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'cc:2**0': 0.24; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'subject:list': 0.30; 'message-id:@mail.gmail.com': 0.30; 'that.': 0.31; '>>>>': 0.31; 'once,': 0.31; 'probably': 0.32; 'subject:the': 0.34; 'except': 0.35; 'case,': 0.35; 'received:google.com': 0.35; 'doing': 0.36; 'list': 0.37; 'though,': 0.39; 'either': 0.39; 'skip:x 10': 0.40; 'how': 0.40; "you're": 0.61; 'subject:get': 0.81; 'to:none': 0.92
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=2QxORaQTPkYhyZDkyGWDXaD/jTChItZOKdlGNV59qHM=; b=nlELGQLqqD5o/ImyeBChLEZWc6Gu9qWRkOm2cge3jrxOzZtGM+niwFRFp/8HeUxpoZ BC131Dc9ZFRy9OIR6UQKqDVkvxPHKsVO1YZ+Ctaf1ADu0y4/pkonOx6qJWJymZv+0Dmn I62UOrjhMqei4GYy1Y16jZXHNmJxu6M0lOdl5TfGJ3TKfhLlB9uE9lSmFyN336fF2B3i hy4utgdqHuXbwyuwIUMrhnSs4rVi0uE8Cs/3ushVbU9ad6vgzVeBUuCfNL1Jf4t/HR+s zYw2J5qkReI6irlP1CtcOdY7XDJKjADUnKSyFaAYglz3Zm+inTyK88sKDQHSwUllZyFr YciA==
MIME-Version 1.0
X-Received by 10.42.25.141 with SMTP id a13mr16558076icc.37.1407552403243; Fri, 08 Aug 2014 19:46:43 -0700 (PDT)
In-Reply-To <53E65BFE.1060309@gmail.com>
References <53E658CD.5020904@gmail.com> <CACwCsY7icE7X2upr0auNhe37xh5HdC09Ad4yaetqpo+fcoBRYg@mail.gmail.com> <53E65BFE.1060309@gmail.com>
Date Sat, 9 Aug 2014 12:46:43 +1000
Subject Re: how to get the ordinal number in list
From Chris Angelico <rosuav@gmail.com>
Cc "python-list@python.org" <python-list@python.org>
Content-Type text/plain; charset=UTF-8
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.12781.1407552411.18130.python-list@python.org> (permalink)
Lines 31
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1407552411 news.xs4all.nl 2836 [2001:888:2000:d::a6]:33014
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:75926

Show key headers only | View raw


On Sun, Aug 10, 2014 at 3:35 AM, luofeiyu <elearn2014@gmail.com> wrote:
>>>> x=["x1","x3","x7","x5","x3"]
>>>> x.index("x3")
> 1
> if i want the result of 1 and 4 ?

Want to know what you can do with some object? Try this:

>>> help(x)

In this case, though, I suspect there's no built-in to search for
*all* of the occurrences of something, so you're best doing your own
loop. You can either iterate straight over the list with enumerate, as
you were doing, or you can use index() with its start argument:

>>> def find_all(lst, obj):
    indices = [-1]
    try:
        while True:
            indices.append(lst.index(obj, indices[-1]+1))
    except ValueError:
        return indices[1:]

>>> x=["x1","x3","x7","x5","x3"]
>>> find_all(x, "x3")
[1, 4]

It's probably cleaner to just iterate over the list once, though, and
you already know how to do that.

ChrisA

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


Thread

Re: how to get the ordinal number in list Chris Angelico <rosuav@gmail.com> - 2014-08-09 12:46 +1000

csiph-web