from __future__ import print_function # REQUIREMENT: # Count the number of times each atom name appears # # Open input file # f = open("3FX2.pdb") # # For each ATOM record, increment count # for the atom name # counts = {} for line in f: if not line.startswith("ATOM"): continue atom_name = line[12:16] counts[atom_name] = counts.get(atom_name, 0) + 1 # # Close input file # f.close() # # Print number of occurrences of each atom name # print("Unsorted") for atom_name, count in counts.items(): print(atom_name, count) # # Print number of occurrences of each atom name # sorted by atom name # print("Sorted by atom name") for atom_name in sorted(counts.keys()): print(atom_name, counts[atom_name]) # # Print number of occurrences of each atom name # sorted by occurrence count # print("Sorted by occurrence count") for count, atom_name in sorted([ (count, atom_name) for atom_name, count in counts.items() ]): print(atom_name, count) # # Print number of occurrences of each atom name # reverse sorted by occurrence count # print("Reverse sorted by occurrence count") for count, atom_name in reversed(sorted( [ (count, atom_name) for atom_name, count in counts.items() ])): print(atom_name, count)