Implementing this algorithm on Lists - Python
<p>Edit: Seems I made a mistake on my previous description and example so here it is fixed.</p>

<h1>Latest Version</h1>

<pre><code>interactions = [ ['O1', 'O3'],
                 ['O2', 'O5'],
                 ['O8', 'O10']
                 ['P3', 'P5'],
                 ['P2', 'P19'],
                 ['P1', 'P6'] ]
</code></pre>

<p>So same as before, each entry is an interaction between two parts of an object. For example think of O and P as organisms, and O1, O8, P4, P6 ... as sub-section to the organisms. So each interaction is between sub-sections in the same organism, and in this list there are many organisms.</p>

<p>Now, the similar list:</p>

<pre><code>similar = ['O1', 'P23'],
          ['O3', 'P50'],
          ['P2', 'O40'],
          ['P19', 'O22']
</code></pre>

<p>So <strong>O1</strong> is similar to <strong>P23</strong> and <strong>O3</strong> is similar to <strong>P50</strong> AND [O1, O2] interact thus the interaction ['P23', 'P50'] is a transformed interaction.</p>

<p>Likewise, <strong>P2</strong> is similar to <strong>O40</strong> and <strong>P19</strong> is similar to <strong>O22</strong> AND [P2, P19] interact thus the interaction ['O40', 'O22'] is a transformed interaction.</p>

<p>The transformed interactions will always be from the same organism, eg: [PX, PX] or [OX, OX].</p>

<h1>Older Version</h1>

<p>Let's say I have the following list:</p>

<pre><code>interactions = [ [O1, O3],
                 [O1, O8],
                 [O4, O6],
                 [O9, O2],
                 [ ... ] ]
</code></pre>

<p>what this list is meant to represent is an interaction between two objects, so <code>O1 and O3</code> interact, etc.</p>

<p>Now, let's say I have a second list:</p>

<pre><code>similar = [ [O1, H33],
            [O6, O9],
            [O4, H1],
            [O2, H12],
            [ ... ] ]
</code></pre>

<p>and what this list is meant to represent is objects that are similar. </p>

<p>If we know objects A and B in the list <code>interactions</code> do indeed have an intereaction, AND we know that we have an object A' that is similar to A, and an object B' which is similar to B, then we can map the interaction from A to B to the objects A' to B'.</p>

<p>For example:
    <code>O9 and O2 interact.</code>
    <code>O6 is similar to O9.</code>
    <code>H12 is similar to O2.</code>
    <code>thus [O6, H12] interact.</code></p>

<p>Note:
<code>interactions = [ [O1, O3] ]</code> is the same as <code>[O3, O1]</code>, although it would only be stored in the list <code>interactions</code> once, in either format. The same applies to the list <code>similar</code>.</p>

<p>So I guess the algorithm for this would be:</p>

<ol>
<li>for every unique object A in the in fields [0] and [1] in list <code>similar</code>,</li>
<li>fetch a list B of interactions from the list <code>interactions</code>.</li>
<li>check for entries in <code>similar</code> where A is similar to some object A', and B is similar to some object B'.</li>
<li>map the interaction between A' and B'.</li>
</ol>

<p>Edit: Code for this version.</p>

<h1>Code</h1>

<pre><code>from collections import defaultdict
interactions = [ ['O1', 'O3'],
                 ['O1', 'O8'],
                 ['O4', 'O6'],
                 ['O9', 'O2'] ]

similar = [ ['O1', 'H33'],
            ['O6', 'O9'],
            ['O4', 'H1'],
            ['O2', 'H12'] ]

def list_of_lists_to_dict(list_of_lists):
  d = defaultdict(list)
  for sublist in list_of_lists:
    d[sublist[0]].append(sublist[1])
    d[sublist[1]].append(sublist[0])
  return d

interactions_dict = list_of_lists_to_dict(interactions)
similar_dict = list_of_lists_to_dict(similar)


for key, values in interactions_dict.items():
  print "{0} interacts with: {1}".format(key, ', '.join(values))
    if key in similar_dict:
      print " {0} is similar to: {1}".format(key, ', '.join(similar_dict[key]))
      forward = True
  for value in values:
    if value in similar_dict:
      print " {0} is similar to: {1}".format(value, ', '.join(similar_dict[value]))
      reverse = True
      if forward and reverse:
        print "     thus [{0}, {1}] interact!".format(', '.join(similar_dict[key]), 
             ',  '.join(similar_dict[value]))
  forward = reverse = False
</code></pre>

<p>Alright, that's all the background information.</p>

<p>So, I'm pretty new to python and I think I could implement this with a bunch of nested for loops and conditions, however I was wondering if there is a more elegant, pythonic way of going about this.</p>

<p>If read all of this, thank you for you time! :)</p>
