Indentifying the LAST occurrence of an item in a list

mkPyVS mikeminer53 at hotmail.com
Fri Apr 6 19:25:42 EDT 2007


On Apr 5, 6:37 pm, "John Machin" <sjmac... at lexicon.net> 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 space was static but the
pointer to the start of the list was moving-> shame on me (what
language is this now ;)!

Couldn't resist the temptation to just collect all of the locations as
I traversed

m = [2,9,1,5,6,3,1,1,9,2]
f = 1        #What we're looking for
location = 0 #Start at beginning of list
fIndexs = []
while 1:
   try:
      location = m.index(f,location) + 1
      fIndexs.append(location-1)
   except ValueError:
      break

print("Last location = %d" % fIndexs[-1])
print("All Items = %s" % fIndexs)




More information about the Python-list mailing list