我在使用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会尝试转换它?我该怎么做才能解决这个错误?
答案 0 :(得分:2)
如果你想要一个字符串数组,你需要指定:
empty([2], dtype="S10") #10-character strings