I have a similar question to this question: Determine if 2 lists have the same elements, regardless of order?
What is the best/quickest way to determine whether an unsorted list list1
is contained in a 'list of lists' myListOfLists
, regardless of the order to the elements in list1
? My attempt is wrapped up in the function doSomething(...)
which I call many times:
def doSomething(myListOfLists, otherInputs):
list1 = []
... # do something here with `otherInputs'
... # which gives `list1' some values
# now only append `list1' to `myListOfLists' if it doesn't already exist
# and if it does exist, remove it
removeFromList = False
for myList in myListOfLists:
if sorted(list1) == sorted(myList):
removeFromList = True
break
if removeFromList:
myListOfLists.remove(list1)
else:
myListOfLists.append(list1)
return myListOfLists
The problem with this is that I need to run the function doSomething(...)
approximately 1.0e5 times. As myListOfLists
gets bigger with every call of doSomething(...)
this becomes massively time consuming.
EDIT:
Some clarification of the task. Let me give an example of the desired output here:
a = []
doSomething(a, [1,2,3])
>> a = [1,2,3]
Because [1,2,3]
is not in a
, it is appended to a
.
doSomething(a, [3,4,5])
>> a = [[1,2,3], [3,4,5]]
Because [3,4,5]
is not in a
, it is appended to a
.
doSomething(a, [1,2,3])
>>[3,4,5]
Because [1,2,3]
is in a
, it is removed from a
.
EDIT:
All lists have the same length.
You can use sets here:
Update 1: Any Sublist contains exactly same items as
otherInputs
.Update 2:
otherInputs
is a subset of any of the sublist:This algorithm appears to be slightly faster:
For 1000000 loops, this takes 1.25399184227 sec on my computer, whilst
takes 1.9238319397 sec.
If you sort given list and then append it to
myListOfLists
you can use this:Use sets