Python 如何修复“TypeError: len() of unsized object”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40142166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:11:57 来源:igfitidea点击:
How to fix "TypeError: len() of unsized object"
提问by Xabier Garcia Andrade
I am getting:
我正进入(状态:
TypeError: len() of unsized object
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?
我怎样才能解决这个问题?
回答by Moses Koledoye
Use the arrays' size
attribute instead:
改用数组的size
属性:
nv = v.size
nu = u.size
You also probably want to use numpy.fromstring
to take and convert the input string into an array:
您可能还想使用numpy.fromstring
将输入字符串转换为数组:
>>> 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