将ndarray转换为字符串数组

发布于 2025-02-03 12:56:18 字数 535 浏览 2 评论 0原文

假设有数组

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']]

Lets say there is 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]]

now I want to convert this to

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']]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一紙繁鸢 2025-02-10 12:56:18

假设您确实有一个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')

Assuming you really have a numpy array (not a list of list), you can use 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)

output:

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')
別甾虛僞 2025-02-10 12:56:18

如果它不是一个numpy数组,它是值列表的列表,以下代码应起作用:

for index in range(len(values)):
   values[index] = [str(num) for num in values[index]]
print(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']]

If it's not a numpy array and it is a list of a list of values the following code should work:

for index in range(len(values)):
   values[index] = [str(num) for num in values[index]]
print(values)

For each list it returns a list of each of the values changed to a string, this returns the following.

[['116.17265886', '39.92265886', '116.1761427', '39.92536232'], 
['116.20749721', '39.90373467', '116.21098105', '39.90643813'], 
['116.21794872', '39.90373467', '116.22143255', '39.90643813']]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文