你知道为什么numpy整数数组的表示没有填充:
>>> print array([[1, 2], [3, 4]])
[[1 2]
[3 4]]
虽然numpy浮点数组在每个条目之前有一个额外的空格?
>>> print array([[1., 2], [3, 4]])
[[ 1. 2.]
[ 3. 4.]]
(至少在Python 2.7中)
我对它背后的原因/想法更感兴趣,而不是导致它的具体实现细节。
答案 0 :(得分:2)
我看了blame
,找出作者为什么这样做。
查看https://github.com/numpy/numpy/blame/master/numpy/core/arrayprint.py
的第584行self.max_str_len = len(str(int(max_val))) + precision + 2
由于幻数2
,空格会添加到float
值前面。
在第593行附近,format
被确定为
if self.sign:
format = '%#+'
else:
format = '%#'
format = format + '%d.%df' % (self.max_str_len, precision)
例如,max_val
为4.0
,precision
为0
。
因此self.max_str_len
将为3
,然后format
将为%#3.0f
最后,打印的值是
print '%#3.0f' % 4.
# 4.
答案 1 :(得分:2)
尝试以下方法: numpy.set_printoptions(sign ='')
答案 2 :(得分:1)
您可以使用np.set_printoptions()
更改这些默认值。这样做的:
np.set_printoptions(formatter={'int_kind': lambda x:' {0:d}'.format(x)})
会将整数数组的行为更改为更接近float数组的行为:
print(np.array([[1, 2], [3, 4]]))
#[[ 1 2]
# [ 3 4]]
答案 3 :(得分:0)
这将帮助您:
import numpy as np
x = np.array(['dev', 'roy', 'kinder'], dtype=np.str)
print("Original Array:")
print(x)
p = np.char.join(" ", x)
print(p)