python - 将字符串字典转换为 numpy 数组

标签 python arrays string python-3.x numpy

我必须更改一个类似这样的字符串字典。

result = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
          'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
          'low-L': ' ', 'low-M': ' ', 'low-R': ' '}

现在,我必须将其转换为 3x3 数组。

到目前为止,我已经得到了一个代码,它几乎可以实现我的预期,但仍然很困惑。

这是代码。

import numpy as np
names = ['left','middle']
formats = ['S3','S3']
dtype = dict(names = names, formats=formats)
array = np.fromiter(result.items(), dtype=dtype, count=len(result))
arr = np.reshape(array, (3,3))
print(repr(arr))
print (arr[0][1]) 

以及生成的输出。

array('lo[[(b'top', b' '), (b'top', b' '), (b'top', b' ')],
       [(b'mid', b' '), (b'mid', b' '), (b'mid', b' ')],
       [(b'low', b' '), (b'low', b' '), (bw', b' ')]],
      dtype=[('left', 'S3'), ('middle', 'S3')])
(b'top', b' ')

注意 print (arr[0][1]) 生成了预期之外的 (b'top', b' ')

这段代码可能有问题,请大家提出建议。

最佳答案

您需要考虑的第一件事是,较低版本的 Python-3.7 中的字典不保留其项目的顺序。因此,如果您使用这些版本之一,您就不能指望得到符合您预期顺序的结果。

话虽如此,一般来说,在 Numpy 数组中保留字符串项的一种非常优化且方便的方法是使用 numpy.chararray() 对象。正如 documentation 中也提到的那样chararrays 提供了字符串和 unicode 值数组的便捷 View 。

以下是如何使用 chararray 获取所需的数组:

>>> items = list(result.items())
# By passing `itemsize=5` to the chararray function you're specifying
# the length of each array item
>>> arr = np.chararray((len(items), 2), itemsize=5)
>>> arr[:] = items
>>> arr
chararray([[b'top-L', ''],
           [b'top-M', ''],
           [b'top-R', ''],
           [b'mid-L', ''],
           [b'mid-M', ''],
           [b'mid-R', ''],
           [b'low-L', ''],
           [b'low-M', ''],
           [b'low-R', '']], dtype='|S5')
>>> arr[0]
chararray([b'top-L', ''], dtype='|S5')
>>> arr[0][1]
''

此代码已在 Python-3.7 交互式 shell 环境中运行,这就是数组的顺序与字典的项目顺序相同的原因。

关于python - 将字符串字典转换为 numpy 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50152770/

相关文章:

python - base64编码是否对输入进行哈希处理?

java - 计算多篇文章中的关键字出现次数

python - 如何在 NumPy 数组中的特定列中乘以标量?

java - Spring查询字符串简单验证?

python - 从列表列表中删除所有出现的特定值 python

python - python 的 shutil.move() 在 linux 上是原子的吗?

python - Python 如何处理全局变量?

ios - 获取唯一对象数组

.net - 成熟且健壮的.NET类,允许/禁止XHTML标记/属性

Python:找到第一个不匹配的字符