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


Groups > comp.lang.python > #75921 > unrolled thread

how to get the ordinal number in list

Started byluofeiyu <elearn2014@gmail.com>
First post2014-08-09 10:22 -0700
Last post2014-08-10 16:56 -0700
Articles 5 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  how to get the ordinal number in list luofeiyu <elearn2014@gmail.com> - 2014-08-09 10:22 -0700
    Re: how to get the ordinal number in list Johannes Bauer <dfnsonfsduifb@gmx.de> - 2014-08-09 14:13 +0200
    Re: how to get the ordinal number in list Rustom Mody <rustompmody@gmail.com> - 2014-08-10 10:45 -0700
      Re: how to get the ordinal number in list Rustom Mody <rustompmody@gmail.com> - 2014-08-10 11:00 -0700
    Re: how to get the ordinal number in list ismeal shanshi <stuffstorehouse2014@gmail.com> - 2014-08-10 16:56 -0700

#75921 — how to get the ordinal number in list

Fromluofeiyu <elearn2014@gmail.com>
Date2014-08-09 10:22 -0700
Subjecthow to get the ordinal number in list
Message-ID<mailman.12776.1407550960.18130.python-list@python.org>
 >>> x=["x1","x3","x7","x5"]
 >>> y="x3"

  how can i get the ordinal number by some codes?

for id ,value in enumerate(x):
     if y==value : print(id)

Is more simple way to do that?

[toc] | [next] | [standalone]


#75942

FromJohannes Bauer <dfnsonfsduifb@gmx.de>
Date2014-08-09 14:13 +0200
Message-ID<ls538p$8c4$1@news.albasani.net>
In reply to#75921
On 09.08.2014 19:22, luofeiyu wrote:
>>>> x=["x1","x3","x7","x5"]
>>>> y="x3"
> 
>  how can i get the ordinal number by some codes?
> 
> for id ,value in enumerate(x):
>     if y==value : print(id)
> 
> Is more simple way to do that?

print(x.index(y))

HTH,
Johannes

-- 
>> Wo hattest Du das Beben nochmal GENAU vorhergesagt?
> Zumindest nicht öffentlich!
Ah, der neueste und bis heute genialste Streich unsere großen
Kosmologen: Die Geheim-Vorhersage.
 - Karl Kaos über Rüdiger Thomas in dsa <hidbv3$om2$1@speranza.aioe.org>

[toc] | [prev] | [next] | [standalone]


#75993

FromRustom Mody <rustompmody@gmail.com>
Date2014-08-10 10:45 -0700
Message-ID<2a9467ba-44e1-47b0-8adc-a0a3833bb209@googlegroups.com>
In reply to#75921
On Saturday, August 9, 2014 7:53:22 AM UTC+5:30, luofeiyu wrote:
> >>> x=["x1","x3","x7","x5"]
>  >>> y="x3"

>   how can i get the ordinal number by some codes?

> for id ,value in enumerate(x):
>      if y==value : print(id)

> Is more simple way to do that?

I feel a bit discourteous going on a tangent and ignoring the OP...

To the OP:

The problem with your code is not that its not simple
but that it uses a print statement

To expunge the print statement you must pay a small price:
Wrap it in a function:

def search(x,y):	
  for id ,value in enumerate(x):
      if y==value : print(id)

>>> search(["x1","x2","x3"], "x2")
1

>>> # Change print to return
>>> def search(x,y):    
...    for id ,value in enumerate(x):
...        if y==value : return id
... 
>>> search(["x1","x2","x3"], "x2")
1
>>> # Works the same (SEEMINGLY)

... # Now change the return to an yield
... 
>>> def search(x,y):    
...    for id ,value in enumerate(x):
...        if y==value : yield id
... 
>>> search(["x1","x2","x3", "x2", "x5", "x2"], "x2")
<generator object search at 0x7f4e20798280>
>>> # Hmm wazzat?!
... list(search(["x1","x2","x3", "x2", "x5", "x2"], "x2"))
[1, 3, 5]

[toc] | [prev] | [next] | [standalone]


#75994

FromRustom Mody <rustompmody@gmail.com>
Date2014-08-10 11:00 -0700
Message-ID<f230e425-68e1-4940-8cbb-f63faeb2e3f3@googlegroups.com>
In reply to#75993
Pressed Send to early.

On Sunday, August 10, 2014 11:15:03 PM UTC+5:30, Rustom Mody wrote:
> >>> # Works the same (SEEMINGLY)

> ... # Now change the return to an yield
> ... 
> >>> def search(x,y):    
> ...    for id ,value in enumerate(x):
> ...        if y==value : yield id
> ... 
> >>> search(["x1","x2","x3", "x2", "x5", "x2"], "x2")
> >>> # Hmm wazzat?!
> ... list(search(["x1","x2","x3", "x2", "x5", "x2"], "x2"))
> [1, 3, 5]

Now you can of course use that to print if you choose

>>> for x in search(["x1","x2","x3"], "x2"):
...   print x
1


But you can also put some other possibly more complex and useful action there
in place of the print if you choose.

With the print version that's not an option.

[toc] | [prev] | [next] | [standalone]


#76011

Fromismeal shanshi <stuffstorehouse2014@gmail.com>
Date2014-08-10 16:56 -0700
Message-ID<b47aed6e-ffa8-4612-9d4b-1838c6274750@googlegroups.com>
In reply to#75921
Herion, Ketamine,Actavis promethazine codeine 16oz and 32oz available , Oxycontine Hydrocodone xanax and medicated

marijuana US- free shipping and other related products for sell at competitive prices.We do world wide shipping to any clear

address.Delivery is 100% safe due to our discreetness and experience.Try our quality and experience then have a story to tell

another day.

Email Address:stuffstorehouse2014 (AT) gmail.com
CONTACT NUMBER: (407)-4852249 ***** Pleas text me only 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web