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


Groups > comp.lang.python > #75994

Re: how to get the ordinal number in list

Newsgroups comp.lang.python
Date 2014-08-10 11:00 -0700
References <mailman.12776.1407550960.18130.python-list@python.org> <2a9467ba-44e1-47b0-8adc-a0a3833bb209@googlegroups.com>
Message-ID <f230e425-68e1-4940-8cbb-f63faeb2e3f3@googlegroups.com> (permalink)
Subject Re: how to get the ordinal number in list
From Rustom Mody <rustompmody@gmail.com>

Show all headers | View raw


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.

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


Thread

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

csiph-web