[Tutor] how to unique the string

lina lina.lastname at gmail.com
Sun Oct 23 11:39:27 CEST 2011


The updated one -- following Alan's advice.

#!/usr/bin/python3

import os.path

mapping={}


DICTIONARYFILE="dictionary.pdb"
TOBETRANSLATEDFILEEXT=".out"
OUTPUTFILEEXT=".txt"

def generate_dict(dictionarysourcefile):
    for line in open(dictionarysourcefile,"r"):
        parts=line.strip().split()
        mapping[parts[2]]=parts[0]


def translate_process(dictionary,tobetranslatedfile):
    results=[]
    unique={}
    for line in open(tobetranslatedfile,"r"):
        tobetranslatedparts=line.strip().split()
        results.append(dictionary[tobetranslatedparts[2]])
    for residue in results:
        unique[residue]=unique.get(residue,0)+1
    for residue, numbers in unique.items():
        print(residue,numbers)
        with open(base+OUTPUTFILEEXT,"w") as f:
            f.write(str(unique))


if __name__=="__main__":
    generate_dict(DICTIONARYFILE)
    for infilename in os.listdir("."):
        base, ext =  os.path.splitext(infilename)
        if ext == TOBETRANSLATEDFILEEXT:
            translate_process(mapping, infilename)


More information about the Tutor mailing list