将 ndarray 转换为字符串数组
Convert ndarray to string array
假设有数组
values = [[ 116.17265886, 39.92265886, 116.1761427 , 39.92536232],
[ 116.20749721, 39.90373467, 116.21098105, 39.90643813],
[ 116.21794872, 39.90373467, 116.22143255, 39.90643813]]
现在我想将其转换为
values = [[ '116.17265886', '39.92265886', '116.1761427' , '39.92536232'],
[ '116.20749721', '39.90373467', '116.21098105', '39.90643813'],
[ '116.21794872', '39.90373467', '116.22143255', '39.90643813']]
假设你真的有一个 numpy 数组(不是列表的列表),你可以使用 astype(str)
:
values = np.array([[ 116.17265886, 39.92265886, 116.1761427 , 39.92536232],
[ 116.20749721, 39.90373467, 116.21098105, 39.90643813],
[ 116.21794872, 39.90373467, 116.22143255, 39.90643813]])
out = values.astype(str)
输出:
array([['116.17265886', '39.92265886', '116.1761427', '39.92536232'],
['116.20749721', '39.90373467', '116.21098105', '39.90643813'],
['116.21794872', '39.90373467', '116.22143255', '39.90643813']],
dtype='<U32')
如果它不是 numpy 数组,而是值列表的列表,则以下代码应该有效:
for index in range(len(values)):
values[index] = [str(num) for num in values[index]]
print(values)
对于每个列表,它returns一个列表的每个值都更改为一个字符串,这returns以下。
[['116.17265886', '39.92265886', '116.1761427', '39.92536232'],
['116.20749721', '39.90373467', '116.21098105', '39.90643813'],
['116.21794872', '39.90373467', '116.22143255', '39.90643813']]
假设有数组
values = [[ 116.17265886, 39.92265886, 116.1761427 , 39.92536232],
[ 116.20749721, 39.90373467, 116.21098105, 39.90643813],
[ 116.21794872, 39.90373467, 116.22143255, 39.90643813]]
现在我想将其转换为
values = [[ '116.17265886', '39.92265886', '116.1761427' , '39.92536232'],
[ '116.20749721', '39.90373467', '116.21098105', '39.90643813'],
[ '116.21794872', '39.90373467', '116.22143255', '39.90643813']]
假设你真的有一个 numpy 数组(不是列表的列表),你可以使用 astype(str)
:
values = np.array([[ 116.17265886, 39.92265886, 116.1761427 , 39.92536232],
[ 116.20749721, 39.90373467, 116.21098105, 39.90643813],
[ 116.21794872, 39.90373467, 116.22143255, 39.90643813]])
out = values.astype(str)
输出:
array([['116.17265886', '39.92265886', '116.1761427', '39.92536232'],
['116.20749721', '39.90373467', '116.21098105', '39.90643813'],
['116.21794872', '39.90373467', '116.22143255', '39.90643813']],
dtype='<U32')
如果它不是 numpy 数组,而是值列表的列表,则以下代码应该有效:
for index in range(len(values)):
values[index] = [str(num) for num in values[index]]
print(values)
对于每个列表,它returns一个列表的每个值都更改为一个字符串,这returns以下。
[['116.17265886', '39.92265886', '116.1761427', '39.92536232'],
['116.20749721', '39.90373467', '116.21098105', '39.90643813'],
['116.21794872', '39.90373467', '116.22143255', '39.90643813']]