【发布时间】:2021-09-28 21:06:02
【问题描述】:
我的字典结构如下:
mydict = {'Sample1': ['0|0','0|1','0|0'],
'Sample2': ['0|0','1|1','0|0'],
'Sample3': ['0|1','0|1','1|0'] }
我想把它转换成这样的数组:
myarray = [['0|0','0|1','0|0'],
['0|0','1|1','0|0'],
['0|1','0|1','1|0']]
我看到here np.fromiter 是我应该使用的功能,但我正在努力使用dtype。
myarray = np.fromiter(mydict.values(), dtype=str) 给了我错误“ValueError:使用可变大小数据类型时必须指定长度。”
我读到here,符号“Sfoo”允许您指定您只使用长度为 foo 的字符串,但是当我尝试myarray = np.fromiter(mydict.values(), dtype='S3) 时,它会产生这个错误,我正在努力解决这个错误:“ValueError : 设置一个带有序列的数组元素。"
【问题讨论】:
标签: python string numpy dictionary