ValueError: 无法将字符串转换为浮点数:"在赋值给np.empty时

0 投票
1 回答
1007 浏览
提问于 2025-04-28 21:50

我在一个使用 numpy 包的脚本中写了以下代码:

self.map_obj_arch_id = np.empty([2])
self.map_obj_arch_id[0] = 'test'

但是当我运行它时,Python 报错了:ValueError: could not convert string to float: test

我明白 Python 不能把 'test' 转换成浮点数,但为什么 Python 会尝试去转换呢?我该怎么做才能解决这个错误呢?

暂无标签

1 个回答

2

如果你想要一个字符串的数组,你需要明确说明这一点:

empty([2], dtype="S10") #10-character strings

撰写回答