一個建議的寫法:def stdDeviation(i, j):L = len(i) (( i, j 假設長度一樣,才能內積mean_i = sum(i)/Lmean_j = sum(j)/Lstd_i = std_j = 0for x in i:std_i += (x - mean_i)**2for x in j:std_j += (x - mean_j)**2std_i = (std_i/L)**0.5std_j = (std_j/L)**0.5inner_product = 0for x, y in zip(i, j):inner_product += x * yreturn std_i, std_j, inner_product
https://i.imgur.com/M3gww9F.png