Counting element symbols in Python -


i'm biologist , i'm quite new in programming, nowadays i'm trying improve; background not informatics.

i`m quite stuck in problem.

we've information molecules; each line begins atom represents 1 atom of entire molecule. example, first 2 lines:

atom      1  n   arg   1       0.609  18.920  11.647  1.00 18.79           n  atom      2  ca  arg   1       0.149  17.722  10.984  1.00 13.68           c 

we supposed count number of distinct atoms; better said, last item of every line (c orn in e.g.)

we have function drives , extract last item, i'm quite stuck @ point, because should write code if don't know atoms find (though know, because have entire list, , have n,c,o , s)

code have:

def count_atom(molecule):      number_atoms = dict()     lines = molecule.split(os.linesep)     line in lines:         if line.startswith('atom'):             atom = line[77].strip()         print atom       return number_atoms  results= count_atoms(molecule) 

molecule represents entire list.

hope understand right, want count occurrence of last char of string?

molecule = '''atom 1 n arg 1 0.609 18.920 11.647 1.00 18.79 n atom 2 ca arg 1 0.149 17.722 10.984 1.00 13.68 c atom 2 ca arg 1 0.149 17.722 10.984 1.00 13.68 se atom 2 ca arg 1 0.149 17.722 10.984 1.00 13.68 pu atom 2 ca arg 1 0.149 17.722 10.984 1.00 13.68 pu atom 2 ca arg 1 0.149 17.722 10.984 1.00 13.68 c'''  def count_atoms(molecule):     number_atoms = dict()     lines = molecule.split(os.linesep)     line in lines:         if line.startswith('atom'):             atom = line.split()[-1].strip()             if number_atoms.get(atom):                 number_atoms[atom] += 1             else:                 number_atoms.update({atom: 1})     return number_atoms  print(count_atoms(molecule)) 

output:

print(count_atoms(molecule)) {'se': 1, 'pu': 2, 'n': 1, 'c': 2} 

Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - IE9 error '$'is not defined -