import scipy as sp
atomos=open("Átomo.txt", 'r')
deltaX=[]
deltaY=[]
deltaZ=[]
d=0
def dist(xf, xi, yf, yi,zf, zi):
d=sp.sqrt((xf-xi)*(xf-xi)+(yf-yi)*(yf-yi)+(zf-zi)*(zf-zi))
return d
for colunas in atomos:
coluna_limpa=colunas.strip().split("\t")
deltaX.append(coluna_limpa[1])
deltaY.append(coluna_limpa[2])
deltaZ.append(coluna_limpa[3])
print(deltaX, deltaY, deltaZ)
d= dist(deltaX[0], deltaX[9],deltaY[0], deltaY[9], deltaZ[0], deltaZ[9] )
print("A distância do segmento formado pelos atomos é de ", d)
File "<ipython-input-5-e2b6e08387d0>", line 11, in dist
d=sp.sqrt((xf-xi)*(xf-xi)+(yf-yi)*(yf-yi)+(zf-zi)*(zf-zi))
TypeError: unsupported operand type(s) for -: 'str' and 'str'
答案 0 :(得分:2)
colunas
作为字符串从文件中读取,并将其拆分将产生一个字符串列表。如果您想将它们用作数字,则必须明确转换它们。 E.g:
coluna_limpa = [float(x) for x in colunas.strip().split("\t")]
答案 1 :(得分:2)
您正在尝试减去字符串,要将字符串转换为整数,请使用int()
。
d = dist(int(deltaX[0]), int(deltaX[9]),
int(deltaY[0]), int(deltaY[9]),
int(deltaZ[0]), int(deltaZ[9]))
或者如果您的号码是浮点数,请使用float()
。
d = dist(float(deltaX[0]), float(deltaX[9]),
float(deltaY[0]), float(deltaY[9]),
float(deltaZ[0]), float(deltaZ[9]))