while trying to play with generator, i was looking for an idea to get
the position of a inner list inside another one, here is my first idea
:
- first find position of first inner element,
- and then see if the slice starting from here is equal to the inner
->
def subPositions(alist, innerlist): if innerlist == []:
return
first, start = innerlist[0], 0
while 1:
try:
p = alist[start:].index(first)
except ValueError:
break # or should i better use return ?
start = start + p
if alist[start: start + len(innerlist)] == innerlist:
yield start, start + len(innerlist)
start += 1
list(subPositions(range(5) + range(5), [2,3]))
[(2, 4), (7, 9)]
maybe have you some better / faster ideas / implementations ? or even
a more pythonic to help me learning that
game2 :) => how can i imagine a way to have the inclusion test rather
be a test upon a regular expression ? i mean instead of checking for
an inner list, rather checking for an "inner regular expression"
matching upon consecutives items of a list ? (btw it isn't still not
really clear in my own mind, but it looks like ideas from here are
always clever one, it may help :)
best,