[英]Getting AttributeError: 'builtin_function_or_method' object has no attribute 'tolist'
import numpy as np
calculations = {}
def calculate(list):
try:
len(list)
array_1 = np.array(list)
except len(list) < 9:
print("List must contain nine numbers")
new_array = array_1.reshape(3, 3)
calculations['mean'] = [(new_array.mean(axis=0).tolist()), (new_array.mean(axis=1).tolist()),
(new_array.mean.tolist())]
calculations['variance'] = [(new_array.var(axis=0).tolist()), (new_array.var(axis=1).tolist()),
(new_array.var().tolist())]
calculations['standard deviation'] = [(new_array.std(axis=0).tolist()), (new_array.std(axis=1).tolist()),
(new_array.std().tolist())]
calculations['max'] = [(new_array.max(axis=0).tolist()), (new_array.max(axis=1).tolist()),
(new_array.max().tolist())]
calculations['min'] = [(new_array.min(axis=0).tolist()), (new_array.min(axis=1).tolist()),
(new_array.min().tolist())]
calculations['sum'] = [(new_array.sum(axis=0).tolist()), (new_array.sum(axis=1).tolist()),
(new_array.sum().tolist())]
return calculations
I'm getting an error我收到一个错误
AttributeError: 'builtin_function_or_method' object has no attribute 'tolist'
You have a mistake in your code over here你的代码有错误在这里
calculations['mean'] = [(new_array.mean(axis=0).tolist()), (new_array.mean(axis=1).tolist()),
(new_array.mean.tolist())]
The new_array.mean.tolist()
is causing the issue as, new_array.mean
will refer to a function, but not call it. new_array.mean.tolist()
导致问题,因为new_array.mean
将引用 function,但不调用它。 So tolist()
will be considered as the atribute of the function.所以
tolist()
将被视为 function 的属性。 Change it to new_array.mean().tolist())
- All is well!将其更改为
new_array.mean().tolist())
- 一切都很好!
--UPDATE-- - 更新 -
This solution works well after the fix.此解决方案在修复后运行良好。 Eg.
例如。
print(calculate([3,2,4,5,6,7,2,1,3]))
produced an output生产了一个 output
{
"mean": [
[
3.3333333333333337,
3.0,
4.666666666666667
],
[
3.0,
6.0,
2.0
],
3.6666666666666667
],
"variance": [
[
1.5555555555555554,
4.666666666666667,
2.888888888888889
],
[
0.6666666666666666,
0.6666666666666666,
0.6666666666666666
],
3.5555555555555555
],
"standard deviation": [
[
1.247219128924647,
2.160246899469287,
1.699673171197595
],
[
0.816496580927726,
0.816496580927726,
0.816496580927726
],
1.8856180831641268
],
"max": [
[
5,
6,
7
],
[
4,
7,
3
],
7
],
"min": [
[
2,
1,
3
],
[
2,
5,
1
],
1
],
"sum": [
[
10,
9,
14
],
[
9,
18,
6
],
33
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.