【发布时间】:2017-03-24 10:18:36
【问题描述】:
我需要编写一个函数来规范化一个向量(找到单位向量)。可以通过将向量的每个单独分量除以其大小来对向量进行归一化。
此函数的输入将是一个向量,即包含 3 个整数的一维列表。
代码如下:
def my_norml(my_list):
tot_sum = 0
for item in my_list:
tot_sum = tot_sum + item**2
magng = tot_sum**(1/2)
norml1 = my_list[0]/magng #here i want to use a for loop
norml2 = my_list[1]/magng
norml3 = my_list[2]/magng
return [norml1, norml2,norml3]
【问题讨论】:
标签: python list python-3.x for-loop python-3.5