Ad

Finding An Exact Position Of A Smaller List Inside A List (python)

- 1 answer

So i Have a list which is something like this:

list=[10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 10.0, 9.9, 10.0, 10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 10.0, 10.0, 10.0, 10.2, 10.0, 9.9, 9.9, 9.9, 9.9, 10.0, 10.2, 10.0, 9.9, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.1, 10.0, 10.0, 10.0, 10.0, 10.0, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.3, 10.3, 10.2, 10.2, 10.3, 10.3, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.3, 10.2, 10.5, 10.9, 10.5, 10.3, 10.3, 10.3, 10.2, 10.2, 10.2, 10.2, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.4, 10.7, 10.3, 10.2, 10.1, 10.1, 10.0, 10.0, 10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 10.0, 9.9, 9.9, 9.9, 10.1, 9.9, 9.9, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.7, 9.8, 9.8, 9.7, 9.7, 9.7, 9.7, 9.7, 9.7, 9.6, 9.7]

And then i also has a sublist which looks something like this:

sublist=[9.9, 9.9, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8]

now what I need to do with this smaller list, is that I have to find where this is located in the main list. So in this case the result should be something like this: index=119 (I might be off with +/-1)

I've been trying to do this all day... And didnt find anything on the web... I have a few ideas:

1) I find the first item of the sublist on the list....which in this case would be 4, so i check the next number which is also correct then the next which will be wrong and it would send it to find another 9.9 in the remaining list[4:] and do the same loop again...until an exact match is found

2) Then another idea is to somehow use strings str(list)[1:-1].find(str(sublist)[1:-1]), which in this case would give the answer of 687...

Problem with these ideas is that they seem to be long and sloppy and also I haven't been able to make these ideas work...

Ad

Answer

How about:

l = [10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 10.0, 9.9, 10.0, 10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 9.9, 9.9, 9.9, 10.0, 10.0, 10.0, 10.2, 10.0, 9.9, 9.9, 9.9, 9.9, 10.0, 10.2, 10.0, 9.9, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.1, 10.0, 10.0, 10.0, 10.0, 10.0, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.3, 10.3, 10.2, 10.2, 10.3, 10.3, 10.2, 10.2, 10.2, 10.2, 10.2, 10.2, 10.3, 10.2, 10.5, 10.9, 10.5, 10.3, 10.3, 10.3, 10.2, 10.2, 10.2, 10.2, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.1, 10.4, 10.7, 10.3, 10.2, 10.1, 10.1, 10.0, 10.0, 10.0, 10.0, 10.0, 9.9, 9.9, 9.9, 10.0, 9.9, 9.9, 9.9, 10.1, 9.9, 9.9, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.7, 9.8, 9.8, 9.7, 9.7, 9.7, 9.7, 9.7, 9.7, 9.6, 9.7]
subl = [9.9, 9.9, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8, 9.8]
for i in xrange(len(l)-len(subl)):
  if l[i:i+len(subl)] == subl:
    print 'found at pos', i
    break
else:
  print 'not found'

This prints found at pos 118.

P.S. I've renamed the variables so that list doesn't shadow the built-in function.

Ad
source: stackoverflow.com
Ad