Here's one approach that starts off by converting the input numeric array to a string array with .astype('S4')
conversion and then does one level of loop comprehension to join all strings in each row with " ".join(item)
, where item
is each row from the string array, like so -
[" ".join(item) for item in x.astype('S4')]
If you are looking for precision control, you can change the dtype
to S6
, S12
or S14
.
Sample run -
In [9]: x
Out[9]:
array([[ 0.62047293, 0.02957529, 0.88920602, 0.57581068],
[ 0.40903378, 0.80748886, 0.83018903, 0.22445871],
[ 0.87296866, 0.94234112, 0.70001789, 0.99333763],
[ 0.96689113, 0.35128491, 0.35775966, 0.26734985]])
In [10]: [" ".join(item) for item in x.astype('S4')]
Out[10]:
['0.62 0.02 0.88 0.57',
'0.40 0.80 0.83 0.22',
'0.87 0.94 0.70 0.99',
'0.96 0.35 0.35 0.26']
In [11]: [" ".join(item) for item in x.astype('S14')]
Out[11]:
['0.620472934011 0.029575285327 0.889206021736 0.575810682998',
'0.409033783485 0.807488858152 0.830189034897 0.224458707937',
'0.872968659668 0.942341118836 0.700017893576 0.993337626766',
'0.966891127767 0.351284905075 0.357759658063 0.267349854182']