2010/7/14 Arnau Sanchez <[email protected]>:
> On Tue, 13 Jul 2010 14:33:09 -0400 Juan Marcelo Leiva Sandoval wrote:
>
>> def buscarIndice(nombre):
>> indice = 0
>> for buscar in listaCalles:
>> if buscar.nombre == nombre:
>> break
>> else:
>> indice = indice + 1
>> return indice
>
> El hilo ha crecido mucho y es difícil de seguir, pero no parece que
> haya salido una solución con enumerate(), next() y generadores. En
> fin, mi idea era ésta (generalizo el atributo):
>
> def get_index(lst, attr, value):
> return next(idx for (idx, obj) in enumerate(lst) if getattr(obj, attr) ==
> value)
>
> que podría llamarse así:
>
> index = get_index(lista_calles, "nombre", "nombre_de_una_calle")
otra opción
nombre = "foo"
try:
calle = itertools.ifilter(lambda c: c.nombre == nombre, listaCalles).next()
idx = listaCalles.index(calle)
except StopIteration:
idx = None
~Rolando
_______________________________________________
Python-es mailing list
[email protected]
http://mail.python.org/mailman/listinfo/python-es
FAQ: http://python-es-faq.wikidot.com/