Edit: It seems I made a mistake in my previous description and example, so it is fixed here.
Latest version
interactions = [ ['O1', 'O3'], ['O2', 'O5'], ['O8', 'O10'] ['P3', 'P5'], ['P2', 'P19'], ['P1', 'P6'] ]
Just as before, each record is an interaction between two parts of an object. For example, think of O and P as organisms, and O1, O8, P4, P6 ... as a subsection of the body. Thus, each interaction occurs between subsections in one organism, and there are many organisms on this list.
Now a similar list:
similar = ['O1', 'P23'], ['O3', 'P50'], ['P2', 'O40'], ['P19', 'O22']
So, O1 is similar to P23 , and O3 is similar to P50 And [O1, O2] interacts in this way, the interaction ['P23', 'P50'] is a transformed interaction.
Similarly, P2 is similar to O40 , and P19 is similar to O22. And [P2, P19] interact, so the interaction ['O40', 'O22'] is a transformed interaction.
Transformed interactions will always be from the same organism, for example: [PX, PX] or [OX, OX].
Old version
Let's say I have the following list:
interactions = [ [O1, O3], [O1, O8], [O4, O6], [O9, O2], [ ... ] ]
what this list is intended to represent is the interaction between two objects, therefore O1 and O3 interact, etc.
Now, let's say I have a second list:
similar = [ [O1, H33], [O6, O9], [O4, H1], [O2, H12], [ ... ] ]
and that this list is for presentation - these are objects that are similar.
If we know that objects A and B in the interactions list do have an interaction, And we know that we have an object A 'similar to A and an object B' that is similar to B, then we can match the interaction from A to B with objects A 'to B'.
For example: O9 and O2 interact. O6 is similar to O9. H12 is similar to O2. thus [O6, H12] interact.
Note: interactions = [ [O1, O3] ] same as [O3, O1] , although it will only be stored in the interactions list once in any format. The same goes for the similar list.
So, I assume that the algorithm for this would be:
- for each unique object A in the fields [0] and [1] in the
similar list, - select the list of interactions B from the
interactions list. - check the entries in
similar , where A is similar to some object A ', and B is similar to some object B'. - display the interaction between A 'and B'.
Edit: Code for this version.
The 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
Good thing all the background information.
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 around this.
If you read all this, thanks for your time! :)