如何修复“TypeError: len() of unsized object" [英] How to fix "TypeError: len() of unsized object"

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

问题描述

我得到:

TypeError: len() of unsized object

运行以下脚本后:

from numpy import *v=array(input('引入联合国向量 v:'))u=array(input('Introduce un vector u: '))nv= len(v)nu=len(u)差异= 0;我=0如果 nv==nu:而我<nv:差异化=差异化 + ((v[i+1]-u[i+1]))**2模数= sqrt(差异)打印('距离',v)别的:打印('不同尺寸的向量')

我该如何解决这个问题?

解决方案

使用数组的 size 属性:

nv = v.sizenu = u.size

<小时>

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

<预><代码>>>>v = np.fromstring(input('输入以逗号分隔的向量元素:'), dtype=int, sep=',')输入以逗号分隔的向量元素:1, 2, 3>>>v数组([1, 2, 3])>>>连(五)3>>>v.尺寸3

I am getting:

TypeError: len() of unsized object

after running the following script:

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

How can I fix this?

解决方案

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() of unsized object"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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