I want to delete those elements of a list that are contained in other elements of the same list. In my case, these are the nodes of several paths. Example:
mylist=[[1,2,3],[1,2,3,4,5,6],[1,2,3,4,5,6,9,10,11],[1,2,3,4,5,6,7,8,9,10],[11,12,13,14,15],[6,5,4,3,2,1]]
Desired Output:
[[1, 2, 3, 4, 5, 6, 9, 10, 11], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [6,5,4,3,2,1]]
I have the following code which works for me but its performance is just too slow. My list contains 12.000 elements, meaning i*j = ca. 144Million.
deleted=0
testlist_new=[]
dummylist = []
for i in range(len(mylist)):
x_dummy_list=[]
dummylist = copy.copy(mylist)
del dummylist[i]
for j in range(len(mylist)-1):
x_dummy_list.append(not Counter(mylist[i]) - Counter(dummylist[j]))
if True in x_dummy_list:
deleted=deleted+1
else:
testlist_new.append(mylist[i])
Resulting in:
testlist_new=[[1, 2, 3, 4, 5, 6, 9, 10, 11], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15]]
So this is the result. For me, it would be okay that [6,5,4,3,2,1] is not there, because the paths are calculated from one point fan-shaped away. But it would be even greater if the Desired Output could be achieved.
I already tried .set as well as issubset functions which do not work for me as they are not fine enough.
Thank you!