我必须更改一个类似这样的字符串字典。
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 中也提到的那样chararray
s 提供了字符串和 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/