Re: Indentifying the LAST occurrence of an item in a list

2007-04-06 Thread John Machin
On Apr 7, 9:25 am, "mkPyVS" <[EMAIL PROTECTED]> wrote: > On Apr 5, 6:37 pm, "John Machin" <[EMAIL PROTECTED]> wrote: > > >>> help(list.index) > > Help on method_descriptor: > > > index(...) > > L.index(value, [start, [stop]]) -> integer -- return first index > > of value > > > I look forward to

Re: Indentifying the LAST occurrence of an item in a list

2007-04-06 Thread mkPyVS
On Apr 5, 6:37 pm, "John Machin" <[EMAIL PROTECTED]> wrote: >>> help(list.index) > Help on method_descriptor: > > index(...) > L.index(value, [start, [stop]]) -> integer -- return first index > of value > > I look forward to your next version. Great point! I was assuming the temp variable spac

Re: Indentifying the LAST occurrence of an item in a list

2007-04-05 Thread John Machin
On Apr 6, 4:55 am, "mkPyVS" <[EMAIL PROTECTED]> wrote: > Not sure below is better but it hacks at larger chunks (excuse the 10 > second coding) > m = [2,9,1,5,6,3,1,1,9,2] > f = 1 > temp = m Overwritten 3 statements later > location = m.index(f) > gloc = location > temp = m[location:] > while 1:

Re: Indentifying the LAST occurrence of an item in a list

2007-04-05 Thread mkPyVS
Not sure below is better but it hacks at larger chunks (excuse the 10 second coding) m = [2,9,1,5,6,3,1,1,9,2] f = 1 temp = m location = m.index(f) gloc = location temp = m[location:] while 1: print(temp) try: location = temp.index(f) + 1 gloc += location except: break

Re: Indentifying the LAST occurrence of an item in a list

2007-04-04 Thread John Machin
On Apr 5, 1:58 am, [EMAIL PROTECTED] wrote: > For any list x, x.index(item) returns the index of the FIRST > occurrence of the item in x. Is there a simple way to identify the > LAST occurrence of an item in a list? My solution feels complex - > reverse the list, look for the first occurence of the

Re: Indentifying the LAST occurrence of an item in a list

2007-04-04 Thread bearophileHUGS
Terry Reedy: > def rindex(lis, item): > for i in range(len(lis)-1, -1, -1): > if item == lis[i]: > return i > else: > raise ValueError("rindex(lis, item): item not in lis") This is an alternative, I don't know if it's faster: def rindex(seq, item): for pos,

Re: Indentifying the LAST occurrence of an item in a list

2007-04-04 Thread Tim Williams
On 4 Apr 2007 08:58:49 -0700, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote: > For any list x, x.index(item) returns the index of the FIRST > occurrence of the item in x. Is there a simple way to identify the > LAST occurrence of an item in a list? My solution feels complex - > reverse the list, look

Re: Indentifying the LAST occurrence of an item in a list

2007-04-04 Thread Terry Reedy
"7stud" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | How about: | | l = [1, 2, 1, 3, 1, 5] | target = 1 | for index, val in enumerate(l): |if val==1: |lastIndexOf = index | | print lastIndexOf You might want to initialize lastIndexOf (to None, for instance). If len(l

Re: Indentifying the LAST occurrence of an item in a list

2007-04-04 Thread 7stud
On Apr 4, 11:20 am, "7stud" <[EMAIL PROTECTED]> wrote: > On Apr 4, 10:55 am, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > > > <[EMAIL PROTECTED]> wrote in message > > >news:[EMAIL PROTECTED] > > | For any list x, x.index(item) returns the index of the FIRST > > | occurrence of the item in x. Is there

Re: Indentifying the LAST occurrence of an item in a list

2007-04-04 Thread 7stud
On Apr 4, 10:55 am, "Terry Reedy" <[EMAIL PROTECTED]> wrote: > <[EMAIL PROTECTED]> wrote in message > > news:[EMAIL PROTECTED] > | For any list x, x.index(item) returns the index of the FIRST > | occurrence of the item in x. Is there a simple way to identify the > | LAST occurrence of an item in a

Re: Indentifying the LAST occurrence of an item in a list

2007-04-04 Thread Terry Reedy
<[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | For any list x, x.index(item) returns the index of the FIRST | occurrence of the item in x. Is there a simple way to identify the | LAST occurrence of an item in a list? My solution feels complex - | reverse the list, look for the firs

Indentifying the LAST occurrence of an item in a list

2007-04-04 Thread tkpmep
For any list x, x.index(item) returns the index of the FIRST occurrence of the item in x. Is there a simple way to identify the LAST occurrence of an item in a list? My solution feels complex - reverse the list, look for the first occurence of the item in the reversed list, and then subtract its in