【问题标题】:Translate numpy array to string array python将numpy数组转换为字符串数组python
【发布时间】:2018-09-19 03:14:11
【问题描述】:

我正在尝试将我的 numpy.ndarray 转换为 python 中的普通字符串数组

我试图通过 toString 转换 numpy 数组来做到这一点,尽管这没有按预期工作。

opencvImage 是数组的名称

import numpy as np

arr = opencvImage
ts = arr.tostring()
aa = np.fromstring(ts)
aa = np.fromstring(arr, dtype=int) 

【问题讨论】:

  • “普通字符串数组”是什么意思?
  • IIUC,你可以使用list(arr.astype(str))
  • 您的问题不清楚。我们不明白你的意图。而且我们不知道结果与您的意图有何不同。
  • @hpaulj 我想将其转换为可以插入到 mySQL 数据库中的列表

标签: python arrays numpy


【解决方案1】:

我相信您正在寻找tolist() 函数

x = np.array([1, 2, 3, 4])
print(type(x)) # output <class 'numpy.ndarray'>
x = x.tolist()
print(type(x)) # output <class 'list'>

【讨论】:

    【解决方案2】:

    tostring 从数组数据缓冲区生成一个字节串。但它不会保存任何dtypeshape 信息。

    为了说明,制作一个简单的 12 位整数数组:

    In [24]: arr = np.arange(12).reshape(3,4)
    In [25]: arr
    Out[25]: 
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])
    In [26]: ts = arr.tostring()
    In [27]: ts
    Out[27]: b'\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00\x07\x00\x00\x00\x08\x00\x00\x00\t\x00\x00\x00\n\x00\x00\x00\x0b\x00\x00\x00'
    

    查看文档,这是tobytes 的另一个名称。您应该可以将此字符串保存为sql

    但重新创建数组需要更多信息:

    In [28]: np.fromstring(ts)
    /usr/local/bin/ipython3:1: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead
      #!/usr/bin/python3
    Out[28]: 
    array([2.12199579e-314, 6.36598737e-314, 1.06099790e-313, 1.48539705e-313,
           1.90979621e-313, 2.33419537e-313])
    

    我们需要指定dtype。默认为float

    In [29]: np.fromstring(ts, dtype=int)
    /usr/local/bin/ipython3:1: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead
      #!/usr/bin/python3
    Out[29]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
    

    注意警告:

    In [30]: np.frombuffer(ts, dtype=int)
    Out[30]: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
    

    但是pickle 可能会更好。它保留了 dtype 和 shape 信息:

    In [34]: import pickle
    In [35]: tss = pickle.dumps(arr)
    In [36]: tss
    Out[36]: b'\x80\x03cnumpy.core.multiarray\n_reconstruct\nq\x00cnumpy\nndarray\nq\x01K\x00\x85q\x02C\x01bq\x03\x87q\x04Rq\x05(K\x01K\x03K\x04\x86q\x06cnumpy\ndtype\nq\x07X\x02\x00\x00\x00i4q\x08K\x00K\x01\x87q\tRq\n(K\x03X\x01\x00\x00\x00<q\x0bNNNJ\xff\xff\xff\xffJ\xff\xff\xff\xffK\x00tq\x0cb\x89C0\x00\x00\x00\x00\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00\x07\x00\x00\x00\x08\x00\x00\x00\t\x00\x00\x00\n\x00\x00\x00\x0b\x00\x00\x00q\rtq\x0eb.'
    In [37]: pickle.loads(tss)
    Out[37]: 
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])
    

    pickle 实际上是使用np.save 来生成字符串。

    【讨论】:

      猜你喜欢
      • 2013-05-05
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      • 2021-04-26
      • 2015-03-28
      • 2016-03-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多