Last active
August 29, 2015 14:23
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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