Despite this seeming an easy task, I didn't find a satisfying way of doing it. There are several solutions based on map or try/except but none of these seems to me solid enough (eg working in a general case and with 2D arrays).
This can be done with pandas, but I would like to avoid importing an entire library just for this task, is it possible to do it just with numpy functions?
To make an example of what I mean, from an array like:
>>a=np.vstack([['zero','one'],np.array(np.arange(2)).T]).astype('|S')
>>print a
[['zero' 'one']
['0' '1']]
the desired output is:
zero one
0 1
You can use a list comprehension within str.join()
:
>>> print '\n'.join([' '.join(i) for i in a])
zero one
0 1
Not sure if this is what you are asking for but a function using regular python can be made to print out the 2d array like you depicted:
def format_array(arr):
for row in arr:
for element in row:
print(element, end=" ")
print('')
return arr
This prints:
zero one
0 1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.