list.index() like...but returning lists (for avoiding '0' onmultiples hits)

G

Gerardo Herzig -Departamento de Proyectos Especial

Hi....i asume this is a veeeery usual question. I'm searching on python.org,
but their results is not giving me the answer that i looking for...So, here i
go...when i have myList = [2,4,2,0] ...myList.index(2) returns 0 (python
2.2)....There is a built-in function-method that returns [0,2]?

Well, sory if RTFM is the answer!!
 
J

John Roth

Srinath Avadhanula said:
but their results is not giving me the answer that i looking for...So, here i
go...when i have myList = [2,4,2,0] ...myList.index(2) returns 0 (python
2.2)....There is a built-in function-method that returns [0,2]?

This works...
a = [1,2,3,4]
a[:2] [1, 2]

But what does that have to do with the question?

As far as I know, there is no such built-in for lists.
You can do something similar with the "re" module
for strings, but not for lists.

John Roth
 
P

Peter Otten

Gerardo Herzig -Departamento de Proyectos Especiales e Internet- Facultad de
Medicina said:
Hi....i asume this is a veeeery usual question. I'm searching on
python.org, but their results is not giving me the answer that i looking
for...So, here i go...when i have myList = [2,4,2,0] ...myList.index(2)
returns 0 (python 2.2)....There is a built-in function-method that returns
[0,2]?

I don't think so. But it's not hard to do it on your own:
.... return [index for (index, item) in enumerate(iterable) if item ==
value]
....
[0, 3]


Peter
 
F

Francis Avila

John Roth wrote in message ...
But what does that have to do with the question?

As far as I know, there is no such built-in for lists.
You can do something similar with the "re" module
for strings, but not for lists.

John Roth

More and more I am finding myself wishing for a neat way to *add*
functionality to a builtin. Not subclass a builtin, but add methods to the
builtin type.

Like (the function does what the OP wants, btw):
.... return [i for i,v in enumerate(self) if v == value]
....
list.indexes = indexes
[0, 2, 2, 3].indexes(2)
[1, 2]

This currently gives:
Traceback (most recent call last):
File "<stdin>", line 1, in ?
list.indexes=indexes
TypeError: can't set attributes of built-in/extension type 'list'


Oh well. Anyway, the following might be faster:

def indices2(L, value):
res = []
last = -1 #below, last+1 to prevent infinite loop.
try:
while True:
last = L.index(value, last+1)
res.append(last)
except ValueError:
return res
 
G

Gerardo Herzig -Departamento de Proyectos Especial

Um...my little brain came with something very much like Peter's example,
without list comprehensions, but really close in terms of performance.

The Francis example work well, and about 3 times faster for python 2.3, but i
cant migrate now (im using 2.2 right now), and the extra parameter on index()
trows exception "index() takes exactly one argument"

I'm reading now the py.docs for that 'end' arg.

Thank you, Peter and Francis!!!!!

Gerardo

El Lun 22 Dic 2003 21:07, Francis Avila escribió:
John Roth wrote in message ...
But what does that have to do with the question?

As far as I know, there is no such built-in for lists.
You can do something similar with the "re" module
for strings, but not for lists.

John Roth

More and more I am finding myself wishing for a neat way to *add*
functionality to a builtin. Not subclass a builtin, but add methods to the
builtin type.

Like (the function does what the OP wants, btw):
... return [i for i,v in enumerate(self) if v == value] ------Francis response...
Oh well. Anyway, the following might be faster:

def indices2(L, value):
res = []
last = -1 #below, last+1 to prevent infinite loop.
try:
while True:
last = L.index(value, last+1)
res.append(last)
except ValueError:
return res
 
D

Dang Griffith

Hi....i asume this is a veeeery usual question. I'm searching on python.org,
but their results is not giving me the answer that i looking for...So, here i
go...when i have myList = [2,4,2,0] ...myList.index(2) returns 0 (python
2.2)....There is a built-in function-method that returns [0,2]?

Well, sory if RTFM is the answer!!
Do you mean a function that returns a list of the indices of all
occurrences of the specified value? I don't think there's a builtin,
but you could do something like this:

def indices(lst, val):
""" Return a list of indices of matches. """
return [i for i in range(0, len(lst)) if lst == val]

myList = [2, 4, 2, 0]
print indices(myList, 2)
# [0, 2]

--dang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top