'numpy.ndarray' to 'str'
时间: 2024-01-12 17:03:46 浏览: 91
numpy.meshgrid()理解(小结)
5星 · 资源好评率100%
To convert a numpy ndarray to a string, you can use the `numpy.array2string()` function. Here's an example:
```python
import numpy as np
arr = np.array([1, 2, 3])
arr_str = np.array2string(arr)
print(arr_str)
```
Output:
```
'[1 2 3]'
```
By default, `array2string()` separates the elements of the array with spaces and uses square brackets to enclose the array. You can modify this behavior by passing additional arguments to the function. For example, you can change the separator to a comma:
```python
import numpy as np
arr = np.array([1, 2, 3])
arr_str = np.array2string(arr, separator=',')
print(arr_str)
```
Output:
```
'[1,2,3]'
```
You can also specify the precision of floating-point numbers and the indentation of the output. See the documentation for `array2string()` for more information on the available options.
阅读全文