Let's say I have the following list
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
I want to find all possible sublists of a certain lenght where they don't contain one certain number and without losing the order of the numbers.
For example all possible sublists with length 6 without the 12 are:
[1,2,3,4,5,6]
[2,3,4,5,6,7]
[3,4,5,6,7,8]
[4,5,6,7,8,9]
[5,6,7,8,9,10]
[6,7,8,9,10,11]
[13,14,15,16,17,18]
The problem is that I want to do it in a very big list and I want the most quick way.
Update with my method:
oldlist = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18]
newlist = []
length = 6
exclude = 12
for i in oldlist:
if length+i>len(oldlist):
break
else:
mylist.append(oldlist[i:(i+length)]
for i in newlist:
if exclude in i:
newlist.remove(i)
I know it's not the best method, that's why I need a better one.
See Question&Answers more detail:
os