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: 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> <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 Cc: "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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 On Sun, Aug 10, 2014 at 3:35 AM, luofeiyu 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