Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: Determine if an unsorted list is contained in a 'list of lists', regardless of the order to the elements
    primarykey
    data
    text
    <p>I have a similar question to this question: <a href="https://stackoverflow.com/questions/8866652/determine-if-2-lists-have-the-same-elements-regardless-of-order">Determine if 2 lists have the same elements, regardless of order?</a></p> <p>What is the best/quickest way to determine whether an unsorted list <code>list1</code> is contained in a 'list of lists' <code>myListOfLists</code>, regardless of the order to the elements in <code>list1</code>? My attempt is wrapped up in the function <code>doSomething(...)</code> which I call many times:</p> <pre class="lang-py prettyprint-override"><code>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 </code></pre> <p>The problem with this is that I need to run the function <code>doSomething(...)</code> approximately 1.0e5 times. As <code>myListOfLists</code> gets bigger with every call of <code>doSomething(...)</code> this becomes massively time consuming.</p> <p>EDIT:</p> <p>Some clarification of the task. Let me give an example of the desired output here:</p> <pre><code>a = [] doSomething(a, [1,2,3]) &gt;&gt; a = [1,2,3] </code></pre> <p>Because <code>[1,2,3]</code> is not in <code>a</code>, it is appended to <code>a</code>.</p> <pre><code>doSomething(a, [3,4,5]) &gt;&gt; a = [[1,2,3], [3,4,5]] </code></pre> <p>Because <code>[3,4,5]</code> is not in <code>a</code>, it is appended to <code>a</code>.</p> <pre><code>doSomething(a, [1,2,3]) &gt;&gt;[3,4,5] </code></pre> <p>Because <code>[1,2,3]</code> <strong>is</strong> in <code>a</code>, it is removed from <code>a</code>.</p> <p>EDIT:</p> <p>All lists have the same length.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload