By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,191 Members | 1,256 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,191 IT Pros & Developers. It's quick & easy.

"Find" in list of objects

P: n/a
Hi!

(Python 2.2.3 if this is relevant :-)

I have a list of objects with, lets say, the attributes "ID", "x" and
"y". Now I want to find the index of list element with ID=10.

Of course I can loop through the list manually, but is there a
construct like

list.find (10, key='ID')

? Thanks for your ideas.

Best regards,
Martin

Oct 23 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Andreas Müller:
is there a construct like
list.find (10, key='ID')
You can create yourself a little convenience function, or you can use
something like the following. First some testing code:

class C:
def __init__(self, id):
self.id = id
def __repr__(self):
return "<%s>" % self.id
seq = map(C, [1, -5, 10, 3])
print seq

That prints:
[<1>, <-5>, <10>, <3>]

Then you can find the index of all the classes with id = 10:
print [i for i, obj in enumerate(seq) if obj.id == 10]

It returns a [2]. If seq doesn't contain the requires object(s) it
returns an empty list.

Or just the first, working lazily:
print (i for i, obj in enumerate(seq) if obj.id == 10).next()

This time if seq doesn't contain the requires object(s) it raises a
StopIteration exception.

You can of course wrap that into a function, using seq and 10 as
arguments.

Bye,
bearophile
Oct 23 '08 #2

P: n/a
En Thu, 23 Oct 2008 05:23:51 -0200, Andreas Müller <hu**********@west.de>
escribió:
(Python 2.2.3 if this is relevant :-)

I have a list of objects with, lets say, the attributes "ID", "x" and
"y". Now I want to find the index of list element with ID=10.

Of course I can loop through the list manually, but is there a
construct like

list.find (10, key='ID')
If you define __eq__ for your objects you may use list.index:

class Obj:
def __init__(self, id_, x=0, y=0):
self.id = id_
self.x = x
self.y = y
def __eq__(self, other):
if not isinstance(other, Obj): raise NotImplementedError
return self.id==other.id

pya = Obj(10)
pyb = Obj(11)
pyc = Obj(12)
pyalist = [a,b,c]
pyprint alist.index(Obj(11))
1
pyprint alist.index(Obj(20))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.index(x): x not in list

Note that this is a "global" change - two separate instances with the same
ID were "not equal" before, and are "equal" now, everywhere, not just
regarding list.index

Of course you always have the old "for" loop solution (`enumerate` would
make things a lot nicer, but it requires 2.3):

def index_by_id(alist, id_):
for i in range(len(alist)):
if alist[i].id==id_:
return i

--
Gabriel Genellina

Oct 23 '08 #3

P: n/a
Andreas Müller:
is there a construct like
list.find (10, key='ID')
Given the current Python a syntax like this is more probable:

somelist.find(10, key=attrgetter('ID'))

Bye,
bearophile
Oct 23 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.