numpy - numpy 中整数数组的符号格式

标签 numpy

对于 float ,numpy.set_printoptions有参数 sign 和选项

  • '-':只打印减号
  • '+':始终打印加号
  • ' ' :打印空格而不是加号

但是,此函数仅影响 float 数组。是否有 int 数组的模拟?

最佳答案

您可以使用formatter将其作为自定义格式传递:

np.set_printoptions(formatter={'int': lambda x: "{:>+}".format(x)})
print(5-np.arange(10))
>>[+5 +4 +3 +2 +1 +0 -1 -2 -3 -4]

关于numpy - numpy 中整数数组的符号格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56073692/

相关文章:

python - 寻找最大派系并删除节点?

python - 使用 Numpy 替换任意轴上的值

python - py2app/Tkinter应用程序错误: "classic environment is no longer supported"

python - 替代 `numpy.tile` 用于周期性掩码

python - 生成用户/项目交互

python - numpy 和 scipy 中的阶乘

python - NumPy 中的向量化操作

python - numpy.where 的嵌套使用

python - 两个 numpy 数组的逐元素减法

python - 为什么函数参数中的元组约定?