merge multiple lines and multiple columns as a line in numpy as str as dtype
时间: 2023-08-13 22:09:08 浏览: 87
合并成多段线
3星 · 编辑精心推荐
To merge multiple lines and multiple columns as a line in numpy as str dtype, you can use the `numpy.ndarray.flatten()` method to flatten the array and then use the `numpy.ndarray.tostring()` method to convert the flattened array to a string.
Here's an example code snippet:
``` python
import numpy as np
# example array
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
# flatten the array
flat_arr = arr.flatten()
# convert the flattened array to a string
str_arr = flat_arr.tostring()
print(str_arr) # b'\x01\x00\x00\x00\x00\x00\x00\x00\x02\x00\x00\x00\x00\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00\x04\x00\x00\x00\x00\x00\x00\x00\x05\x00\x00\x00\x00\x00\x00\x00\x06\x00\x00\x00\x00\x00\x00\x00\x07\x00\x00\x00\x00\x00\x00\x00\x08\x00\x00\x00\x00\x00\x00\x00\t\x00\x00\x00\x00\x00\x00\x00'
```
In this example, `arr` is a 2D array with 3 rows and 3 columns. The `flatten()` method is used to flatten the array into a 1D array, and the `tostring()` method is used to convert the flattened array to a string with dtype `str`. The resulting string is printed to the console.
阅读全文