TypeError: unhashable type: 'numpy.ndarray'
时间: 2023-11-11 19:01:52 浏览: 36
This error message occurs when a numpy array is used as a key in a dictionary or as an element in a set. Numpy arrays are mutable objects and therefore cannot be hashed, which is required for use as a key or element in a hash table.
To fix this error, you can convert the numpy array to a hashable type such as a tuple or a string. For example, if you have a numpy array called `arr`, you can convert it to a tuple using the `tuple` function:
```
arr = np.array([1, 2, 3])
key = tuple(arr)
```
Alternatively, you can convert the numpy array to a string using the `tostring` method:
```
arr = np.array([1, 2, 3])
key = arr.tostring()
```
You can then use the resulting tuple or string as a key or element in your dictionary or set.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![apk](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)