python - 在 Python 中使用 Format 时出现类型错误

标签 python format

我有一个具有以下结构的 numpy.ndarray:

   array([[ 7963.92759169, -2931.3518914 ,  3360.79428745],
   [ 7964.28495515, -2930.99452794,  3361.15165092],
   [ 7965.60367246, -2929.67581063,  3362.47036823]])

我尝试使用以下代码将小数点后的数字限制为 2:

for (label, score) in zip(lables, scoring_fn):
   print("[INFO] {}: {:.2f}".format(label, float(score)))

这样,我收到错误“TypeError:不支持的格式字符串传递给 numpy.ndarray。格式”。谁能建议如何纠正这个错误?

最佳答案

或者使用np.ndarray.round:

arr = arr.round(2)

关于python - 在 Python 中使用 Format 时出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57668612/

相关文章:

c++ - 关于 C++ 格式化输出的查询

php - 如何用php验证图像格式

python - 如何用空格填充 str 列以强制达到所需的长度?

python - Spacy IS_ALPHA 匹配器模式不起作用?

python - 如何创建未链接到原始对象的 python 对象的副本?

python - 将值附加到 Pandas 系列

arrays - 如何避免最后打印nil?

python - 如何对车牌进行格式检查

python - pyparsing部分匹配或递归限制命中

python - 如何在 Google colab 中循环播放音频