如何修复“未大小化对象的TypeError:len()”

2024-06-04 18:16:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到:

未缩放对象的TypeError:len()

运行以下脚本后:

from numpy import *

v=array(input('Introduce un vector v: '))
u=array(input('Introduce un vector u: '))

nv= len(v)
nu= len(u)

diferenza= 0; i=0

if nv==nu:

    while i<nv:
        diferenza=diferenza + ((v[i+1]-u[i+1]))**2

    modulo= sqrt(diferenza)
    print('Distancia', v)
else:
    print('Vectores de diferente dimensión')

我该怎么解决?


Tags: 对象fromnumpy脚本inputlenarraynu
1条回答
网友
1楼 · 发布于 2024-06-04 18:16:35

改为使用数组^{}属性:

nv = v.size
nu = u.size

您可能还希望使用^{}获取输入字符串并将其转换为数组:

>>> v = np.fromstring(input('enter the elements of the vector separated by comma: '), dtype=int, sep=',')
enter the elements of the vector separated by comma: 1, 2, 3
>>> v
array([1, 2, 3])
>>> len(v)
3
>>> v.size
3

相关问题 更多 >