[英]python "TypeError: 'builtin_function_or_method' object is not iterable"
im trying to make a function that calculates the mad in a list but after i was done making it, it didnt work and said 'TypeError: 'builtin_function_or_method' object is not iterable' and i dont know why is said this.我试图制作一个 function 来计算列表中的 mad 但在我完成制作之后,它没有工作并说'TypeError:'builtin_function_or_method'object is not iterable'我不知道为什么这么说。 heres the code.这是代码。
def mad(a):
mean = sum(a)/len(a)
absoluteDeviation = []
for eachValue in input:
absoluteDeviation.append(abs(eachValue - mean))
eachValue += 1
mad = sum(absoluteDeviation)/len(absoluteDeviation)
print(mad)
mad([2,4,6,8])
instead of代替
for eachValue in input:
It should be:它应该是:
for eachValue in a:#since a is the iterable not input
there is no need for:没有必要:
eachValue += 1
def mad(a):
mean = sum(a)/len(a)
absoluteDeviation = []
for eachValue in a:
absoluteDeviation.append(abs(eachValue - mean))
#eachValue += 1
return sum(absoluteDeviation)/len(absoluteDeviation)
print(mad([2,4,6,8]))
$python3.10 iterable.py
2.0
A better approach to calculate mean absolute deviation since we are using less memory as compare to above code:一种更好的计算平均绝对偏差的方法,因为与上面的代码相比,我们使用的 memory 更少:
"""
mean absolute deviation
"""
def mad(a):
mean = sum(a)/float(len(a))
mean_absolute_deviation = 0
for eachValue in a:
mean_absolute_deviation += abs(eachValue - mean)
mean_absolute_deviation /= len(a)
return mean_absolute_deviation
print(mad([2,4,6,8]))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.