Reputation: 6227
Do you know why the representation of numpy array of integers has no padding:
>>> print array([[1, 2], [3, 4]])
[[1 2]
[3 4]]
while the numpy array of floats has one extra space before each of the entries?
>>> print array([[1., 2], [3, 4]])
[[ 1. 2.]
[ 3. 4.]]
(at least in Python 2.7)
I am more interested in the reason/idea behind it, less in the specific implementation details that cause it.
Upvotes: 3
Views: 5011
Reputation: 11
This will help you:
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)
Upvotes: 0
Reputation: 6186
I have looked blame
to find out why authors implement as this.
Take a look at the line 584 of https://github.com/numpy/numpy/blame/master/numpy/core/arrayprint.py
self.max_str_len = len(str(int(max_val))) + precision + 2
Because of the magic number 2
, the space is added to in front of float
values.
Around the line 593, format
is determined as
if self.sign:
format = '%#+'
else:
format = '%#'
format = format + '%d.%df' % (self.max_str_len, precision)
As your example, max_val
is 4.0
, and precision
is 0
.
So self.max_str_len
will be 3
, then format
will be %#3.0f
Finally, the value printed is
print '%#3.0f' % 4.
# 4.
Upvotes: 2