Python のコードを改善したいと思います。より少ないコードでこれと同じ結果を得るために、ここでロジックのヘルプを探しています。
私の手順は、パラメーターを介してアトムの文字列を取得し、それらを「学習」して、学習したアトムのリストを返します。
コードを最適化する方法があるかどうか知りたいです。
def mol_term(molecule):
upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
list_of_atoms = []
for i in range(len(molecule) - 1): #goes all string long
if molecule[i] in upper:
if not molecule[i+1] in upper:
temp = molecule[i] + molecule[i+1] #if atom has two letters
i = i + 1
else:
temp = molecule[i] #if not
if not temp in list_of_atoms:
list_of_atoms.append(temp) #if atom is not in the list appends to it
if molecule[-1] in upper:
list_of_atoms.append(molecule[-1]) #checks last letter
return print(list_of_atoms)
どうもありがとうございました。