'list' object has no attribute 'array'
时间: 2023-11-14 14:06:31 浏览: 24
这个错误是因为Python中的list对象没有array属性。如果你想使用array属性,你需要将list对象转换为numpy数组。你可以使用numpy.array()函数将list转换为数组。如果你想将数组转换回list,你可以使用tolist()函数。
<<引用:numpy.array可使用 shape。list不能使用shape。 array转list:array B B.tolist()即可 。 引用:补充知识:Pandas使用DataFrame出现错误:AttributeError: ‘list’ object has no attribute ‘astype’ 在使用Pandas的DataFrame时出现了错误:AttributeError: ‘list’ object has no attribute ‘astype’ 代码...。>>
如果你想使用numpy的array属性,你需要将list对象转换为numpy数组。你可以使用numpy.array()函数将list转换为数组,例如:
```
import numpy as np
my_list = [1, 2, 3, 4, 5]
my_array = np.array(my_list)
print(my_array.shape)
```
这将输出数组的形状,即(5,)。如果你想将数组转换回list,你可以使用tolist()函数,例如:
```
my_new_list = my_array.tolist()
print(my_new_list)
```
这将输出[1, 2, 3, 4, 5]。