Re: object's list index

2006-03-03 Thread Steven D'Aprano
On Fri, 03 Mar 2006 04:48:20 -0800, Iain King wrote: > I think python only stores lists one way - i.e. each index maps to it's > value, but no backwards trace is kept from value to index. Python lists are arrays of pointers to objects. The objects themselves have no clue what lists they belong to

Re: object's list index

2006-03-03 Thread Sybren Stuvel
Iain King enlightened us with: > i = 0 > for object in list: > objectIndex = i > print objectIndex > i += 1 Why not: for index, object in enumerate(list): print index Sybren -- The problem with the world is stupidity. Not saying there should be a capital punishment for stupidity

Re: object's list index

2006-03-03 Thread Kent Johnson
William Meyer wrote: > hi, > > I need to get the index of an object in a list. I know that no two objects > in the list are the same, but objects might evaluate as equal. for example > > list = [obj1, obj2, obj3, obj4, obj5] > for object in list: > objectIndex = list.index(object) > p

Re: object's list index

2006-03-03 Thread Iain King
Iain King wrote: > Iain King wrote: > > William Meyer wrote: > > > hi, > > > > > > I need to get the index of an object in a list. I know that no two > > > objects > > > in the list are the same, but objects might evaluate as equal. for example > > > > > > list = [obj1, obj2, obj3, obj4, obj5

Re: object's list index

2006-03-03 Thread Felipe Almeida Lessa
Em Sex, 2006-03-03 às 12:48 +, William Meyer escreveu: > Kent Johnson kentsjohnson.com> writes: > > > In either case enumerate() is your friend. To find an > > item by identity: > > > > def index_by_id(lst, o): > >for i, item in enumerate(lst): > > if item is o: > >return i

Re: object's list index

2006-03-03 Thread Iain King
Iain King wrote: > William Meyer wrote: > > hi, > > > > I need to get the index of an object in a list. I know that no two > > objects > > in the list are the same, but objects might evaluate as equal. for example > > > > list = [obj1, obj2, obj3, obj4, obj5] > > for object in list: > > o

Re: object's list index

2006-03-03 Thread William Meyer
Kent Johnson kentsjohnson.com> writes: > In either case enumerate() is your friend. To find an > item by identity: > > def index_by_id(lst, o): >for i, item in enumerate(lst): > if item is o: >return i >raise ValueError, "%s not in list" % o > > If you just want the index

Re: object's list index

2006-03-03 Thread William Meyer
Iain King gmail.com> writes: > what's wrong with: > > i = 0 > for object in list: > objectIndex = i > print objectIndex > i += 1 > > Iain > The issues with that is you might have a complex structure below the for object in list: with lots of continues or breaks and you don't want

Re: object's list index

2006-03-03 Thread Fredrik Lundh
William Meyer wrote: >I need to get the index of an object in a list. I know that no two objects > in the list are the same, but objects might evaluate as equal. for example > > list = [obj1, obj2, obj3, obj4, obj5] > for object in list: >objectIndex = list.index(object) >print objectI

Re: object's list index

2006-03-03 Thread Sebastjan Trepca
Um, what about: for oindex in xrange(len(list)): object = list[oindex] print oindex You can't create a generic function for this. Sebastjan On 3/3/06, William Meyer <[EMAIL PROTECTED]> wrote: > hi, > > I need to get the index of an object in a list. I know that no two objects > in the

Re: object's list index

2006-03-03 Thread Iain King
William Meyer wrote: > hi, > > I need to get the index of an object in a list. I know that no two objects > in the list are the same, but objects might evaluate as equal. for example > > list = [obj1, obj2, obj3, obj4, obj5] > for object in list: > objectIndex = list.index(object) > pr

object's list index

2006-03-03 Thread William Meyer
hi, I need to get the index of an object in a list. I know that no two objects in the list are the same, but objects might evaluate as equal. for example list = [obj1, obj2, obj3, obj4, obj5] for object in list: objectIndex = list.index(object) print objectIndex prints 0, 1, 2, 3, 2