Combining Indices of Two Sorted Lists to Form a Third SuperSorted List
<p>I have two lists <code>['AAPL', 'MMM', 'AMAT']</code> and <code>['AMAT', 'AAPL', 'MMM']</code> and I want to create a third list based on the positions of each string in its respective list.</p>

<p>For example: 
<code>AAPL</code> ranks 1st + 2nd = total 3, <code>MMM</code> ranks 2nd and 3rd = total 5, <code>AMAT</code> ranks 1st and 3rd = total 4. </p>

<p>Final list would be (by decreasing cumulative position) <code>['AAPL', 'AMAT', 'MMM']</code>. </p>

<p>I don't even know where to begin with this. </p>

<p>You can use a dictionary in order to save the items with sum of their indices, then create the expected list based on indices:</p>

<pre><code>&gt;&gt;&gt; from collections import defaultdict
&gt;&gt;&gt; 
&gt;&gt;&gt; d = defaultdict(int)
&gt;&gt;&gt; 
&gt;&gt;&gt; for (ind1, j), (ind2, t) in zip(enumerate(a), enumerate(b)):
...     d[j] += ind1
...     d[t] += ind2
...  
&gt;&gt;&gt; sorted(d.keys(), key=lambda x: d[x])
['AAPL', 'AMAT', 'MMM']
</code></pre>

<p>If lists don't have same length you can use <code>itertools.zip_longet</code> (in python2 <code>izip_longest</code>)in order to <code>zip</code> the enumerate objects.</p>

python list