计算每个元素的原子数

Count the number of atoms of each element

我必须访问一个文件并计算每个元素的原子数。即统计最后一个字符出现的次数。

例如,我有一个名为 14ly.pdb 的文件,其中包含以下行:

ATOM 211 N TYR A 27 4.697 8.290 -3.031 1.00 13.35 N

ATOM 212 CA TYR A 27 5.025 8.033 -1.616 0.51 11.29 C

ATOM 214 C TYR A 27 4.189 8.932 -0.730 1.00 10.87 C 

ATOM 215 O TYR A 27 3.774 10.030 -1.101 1.00 12.90 O

我应该得到结果:'N':1, 'C':2, 'O':1,即1个N型原子,2个C型原子和1个O型原子。

我有以下不完整的代码需要完成:

import os

def count_atoms(pdb_file_name):
  num_atoms = dict()
  with open(pdb_file_name) as file_content:

##Here should go the code I need## 

return num_atoms
result = count_atoms('14ly.pdb')
print(result)
number_of_atoms = dict()
with open("14ly.pdb", "r") as f:
    for line in f:
        line_words = line.split(" ")
        last_char = line_words[-1].rstrip('\n')
        if last_char in number_of_atoms.keys():
            number_of_atoms[last_char] += 1
        else:
            number_of_atoms[last_char] = 1
print(number_of_atoms)

我想这应该够了