如何修复"TypeError:未调整大小的对象的len()"? [英] How to fix "TypeError: len() of unsized object"

查看:98
本文介绍了如何修复"TypeError:未调整大小的对象的len()"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了:

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')

我该如何解决?

推荐答案

使用数组的

Use the arrays' size attribute instead:

nv = v.size
nu = u.size


您可能还想使用 将输入字符串转换为数组:


You also probably want to use numpy.fromstring to take and convert the input string into an array:

>>> 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

这篇关于如何修复"TypeError:未调整大小的对象的len()"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆