Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a brute-force approach (it might be easier to understand):</p> <pre><code>from itertools import chain def condense(*lists): # remember original positions positions = {} for pos, item in enumerate(chain(*lists)): if item not in positions: positions[item] = pos # condense disregarding order sets = condense_sets(map(set, lists)) # restore order result = [sorted(s, key=positions.get) for s in sets] return result if len(result) != 1 else result[0] def condense_sets(sets): result = [] for candidate in sets: for current in result: if candidate &amp; current: # found overlap current |= candidate # combine (merge sets) # new items from candidate may create an overlap # between current set and the remaining result sets result = condense_sets(result) # merge such sets break else: # no common elements found (or result is empty) result.append(candidate) return result </code></pre> <h3>Example</h3> <pre><code>&gt;&gt;&gt; condense([1,2,3], [10,5], [3,8,5]) [1, 2, 3, 10, 5, 8] &gt;&gt;&gt; a = [1,2,3] &gt;&gt;&gt; b = [3,4] &gt;&gt;&gt; i = [21,22] &gt;&gt;&gt; c = [88,7,8] &gt;&gt;&gt; e = [5,4] &gt;&gt;&gt; d = [3, 50] &gt;&gt;&gt; f = [8,9] &gt;&gt;&gt; g= [9,10] &gt;&gt;&gt; h = [20,21] &gt;&gt;&gt; condense(*[a,b,c,i,e,d,f,g,h,a,c,i]*1000) [[1, 2, 3, 4, 5, 50], [88, 7, 8, 9, 10], [21, 22, 20]] &gt;&gt;&gt; condense([1], [2, 3, 2]) [[1], [2, 3]] </code></pre> <h2>Performance comparison</h2> <p><code>condense_*()</code> functions are from the answers to this question. <code>lst_OP</code> input list from the question (different size), <code>lst_BK</code> - the test list from <a href="https://stackoverflow.com/a/13716804/4279">@Blckknght's answer</a> (different size). See <a href="https://gist.github.com/4babbce1179d77272b68/e4d5e174b09a3298766178f252d2c17c5f0619fb#file_measure_performance_condense_lists.py" rel="nofollow noreferrer">the source</a>.</p> <p>Measurements show that solutions based on "disjoint sets" and "connected components of undirected graph" concepts perform similar on both types of input.</p> <pre><code>name time ratio comment condense_hynekcer 5.79 msec 1.00 lst_OP condense_hynekcer2 7.4 msec 1.28 lst_OP condense_pillmuncher2 11.5 msec 1.99 lst_OP condense_blckknght 16.8 msec 2.91 lst_OP condense_jfs 26 msec 4.49 lst_OP condense_BK 30.5 msec 5.28 lst_OP condense_blckknght2 30.9 msec 5.34 lst_OP condense_blckknght3 32.5 msec 5.62 lst_OP name time ratio comment condense_blckknght 964 usec 1.00 lst_BK condense_blckknght2 1.41 msec 1.47 lst_BK condense_blckknght3 1.46 msec 1.51 lst_BK condense_hynekcer2 1.95 msec 2.03 lst_BK condense_pillmuncher2 2.1 msec 2.18 lst_BK condense_hynekcer 2.12 msec 2.20 lst_BK condense_BK 3.39 msec 3.52 lst_BK condense_jfs 544 msec 563.66 lst_BK name time ratio comment condense_hynekcer 6.86 msec 1.00 lst_rnd condense_jfs 16.8 msec 2.44 lst_rnd condense_blckknght 28.6 msec 4.16 lst_rnd condense_blckknght2 56.1 msec 8.18 lst_rnd condense_blckknght3 56.3 msec 8.21 lst_rnd condense_BK 70.2 msec 10.23 lst_rnd condense_pillmuncher2 324 msec 47.22 lst_rnd condense_hynekcer2 334 msec 48.61 lst_rnd </code></pre> <p>To reproduce results <a href="https://gist.github.com/4babbce1179d77272b68" rel="nofollow noreferrer">clone gist</a> and run <a href="https://gist.github.com/4babbce1179d77272b68#file_measure_performance_condense_lists.py" rel="nofollow noreferrer"><code>measure-performance-condense-lists.py</code></a></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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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