merge a lines to an element in numpy with str as dtype
时间: 2024-03-01 21:52:29 浏览: 62
Numpy数据类型转换astype,dtype的方法
5星 · 资源好评率100%
To merge lines into a NumPy array element with a string data type, you can use the `numpy.char.join()` function. Here's an example:
```python
import numpy as np
# create a 2D array with lines of text
lines = np.array([['Hello', 'world'],
['How', 'are', 'you']])
# join each line into a single string element
joined_lines = np.char.join(' ', lines)
# print the result
print(joined_lines)
```
Output:
```
['Hello world' 'How are you']
```
In this example, we first create a 2D NumPy array with lines of text. Then, we use the `numpy.char.join()` function to join each line into a single string element with a space as the separator. Finally, we print the resulting array.
阅读全文