I would like a fast way to solve the following problem: given a list (the order of the elements in the list does not matter but the list cannot have numbers appearing many times). For example,
L=[1, 2, 3, 4, 5, 14, 6, 8, 9, 10, 11, 12, 7, 9, 10, 11, 12, 13]
The length of the list is $mk$ for some $m,k$ (k is fixed in the beginning). I would like to find all m-tuples of lists such that all the numbers in each list are different and each list is ordered from small to large, and there is 0 or 1 gap in each list. For example, [1,2,3,4] does not have gap, and [1,2,5,6] has one gap, and [1,3,6,7] has two gaps. For example,
t1=[[1, 2, 3, 4, 5, 14], [6, 8, 9, 10, 11, 12], [7, 9, 10, 11, 12, 13]]
is a 3-tuple which satisfies all the conditions. Here k=6, m=3.
I wrote the following to try to find all m-tuples which satisfies the condition of all numbers in each list are different. But it is not very fast. Is there some fast way to solve the problem? Thank you very much.
import itertools
import random
from collections import Counter
def SetDifferenceListDifference(a, b):
diff = Counter(a) - Counter(b)
return list(diff.elements())
def removeDuplicatesListOfLists(l): # very fast
l.sort()
r=list(l for l,_ in itertools.groupby(l))
return r
L00=[[1, 2, 3, 4, 5, 14], [6, 8, 9, 10, 11, 12], [7, 9, 10, 11, 12, 13]]
k=len(L00[0])
L=sorted(flatten(L00))
c1=[]
sn=0
m=len(L)/k
while sn<3000:
l=L
temp=len(c1)
c2=[]
while l!=[]:
t1=removeDuplicatesListOfLists(l)
#print(t1)
if len(t1)<k:
break
t2=sorted(random.sample(t1, k))
#print(t2)
c2.append(t2)
l=SetDifferenceListDifference(l,t2)
if len(c2)==m:
c1.append(sorted(c2))
c1=removeDuplicatesListOfLists(c1)
#print(len(c1))
if len(c1)==temp:
sn=sn+1
len(c1)