I have the following two lists:
prefix = ['AGG', 'CAG', 'CAG', 'GAG', 'GGA', 'GGG', 'GGG']
suffix = ['GGG', 'AGG', 'AGG', 'AGG', 'GAG', 'GGA', 'GGG']
I am trying to use defaultdict to get this result:
AGG -> GGG
CAG -> AGG,AGG
GAG -> AGG
GGA -> GAG
GGG -> GGA,GGG
(Yes this is a Rosalind problem for those of you who know. I REALLY want to learn how to use dictionaries effectively though, so I am NOT looking for homework answers. Just dictionary help.)
This is my code:
from collections import defaultdict
nodes = defaultdict(set)
for pre, suf in zip(prefix, suffix):
nodes[pre].add(suf)
for k,v in nodes.items():
print(k,v)
This is my result:
CAG {'AGG'}
GAG {'AGG'}
GGG {'GGG', 'GGA'}
AGG {'GGG'}
GGA {'GAG'}
So I have two issues:
1) I need AGG, the value for CAG, to be preserved as two identical instances.
2) I cannot figure out how to print from my dictionary nicely, without brackets and quotes and to add in the arrow.
Thank you in advance!
Use a defaultdict
of list
, instead of set
. Sets removes duplicates.
Your code is already fine, you'll just have to change the
nodes[pre].add(suf)
to
nodes[pre].append(suf)
For the printing, it will be like
print('{} -> {}'.format(k, ','.join(v)))