Skip to content

Instantly share code, notes, and snippets.

@asalt
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asalt/abfa91b14e72f3f9a823 to your computer and use it in GitHub Desktop.
Save asalt/abfa91b14e72f3f9a823 to your computer and use it in GitHub Desktop.
Simple way to build a dictionary that has a list of lists as values. Good for removing redundancies (keys) but preserving redundancies in the values.
d = {}
with open('myfile.ext','rU') as f:
for line in f:
linesplit = line.split('\t')
try:
d[linesplit[fasta_pos]].append( [x for x in linesplit[other_positions] ) #list comprehension
except KeyError:
d[linesplit[fasta_pos]] = []
d[linesplit[fasta_pos]].append( [x for x in linesplit[other_positions] )
# write to file:
out = open('fileout.ext', 'wb')
for key in d.keys():
# format key and d[key] appropriately
out.write(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment