13

While using scipy/numpy, I do get information that I store into a numpy.ndarray

>>> a
array([[ 0.15555605,  0.51031528,  0.84580176,  0.06722675],
       [ 0.60556045,  0.62721023, -0.48979983, -0.04152777],
       [-0.78044785,  0.58837543, -0.21146041, -0.13568023],
       [ 0.        ,  0.        ,  0.        ,  1.        ]])
>>> print(a)
[[ 0.15555605  0.51031528  0.84580176  0.06722675]
 [ 0.60556045  0.62721023 -0.48979983 -0.04152777]
 [-0.78044785  0.58837543 -0.21146041 -0.13568023]
 [ 0.          0.          0.          1.        ]]

How can I print the result on a single line?

I already checked:

>>> numpy.get_printoptions()
{'precision': 8, 'threshold': 1000, 'edgeitems': 3, 'linewidth': 75, 'suppress': False, 'nanstr': 'nan', 'infstr': 'inf', 'formatter': None}

But even setting linewidth to 1000 does no change this. Is there a way to change the displayed format of that type?

Is it also possible to add comma in between each number (like the array display but without the surrounding array(...))?

1
  • 2
    If you want the array "as-is" printed in one line, you can try print(a.tolist()) May 23, 2017 at 15:52

2 Answers 2

24

In order to print a numpy.array into a single line, you can convert it to a list with its built-in function numpy.tolist()

Example:

import numpy as np

arr = np.array(((1, 2, 3), (4, 5, 6), (7, 8, 9)))

Simple print of array:

print(arr)
[[1, 2, 3]
 [4, 5, 6]
 [7, 8, 9]]

In comparison with numpy.tolist():

print(array.tolist())
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
2
  • 1
    list(arr) does the job more concise.
    – user989762
    May 5, 2018 at 8:11
  • 7
    array.tolist() and list(arr) are not the same. print(list(arr)) gives [array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9])]
    – dloeckx
    Oct 28, 2019 at 22:18
8

NumPy provides a few ways to customize the printing, for example np.array2string.

For this answer I assume you have such an array:

>>> import numpy as np
... arr = np.array([[ 0.15555605,  0.51031528,  0.84580176,  0.06722675],
...                 [ 0.60556045,  0.62721023, -0.48979983, -0.04152777],
...                 [-0.78044785,  0.58837543, -0.21146041, -0.13568023],
...                 [ 0.        ,  0.        ,  0.        ,  1.        ]])
  • If you want to display all items you need to make sure that you set threshold to np.inf.
  • In case you want to have , as separator, you can set separator to ','.

However it doesn't have an option to remove the line breaks, just

  • max_line_width which gives the number of characters printed in one line for the innermost dimension. So it works for 1D arrays when you set max_line_width=np.inf but it doesn't work out of the box for ND arrays.

Fortunately it returns a string that can be manipulated, for example by removing all linebreaks:

>>> np.array2string(arr, threshold=np.inf, max_line_width=np.inf, separator=',').replace('\n', '')
'[[ 0.15555605, 0.51031528, 0.84580176, 0.06722675], [ 0.60556045, 0.62721023,-0.48979983,-0.04152777], [-0.78044785, 0.58837543,-0.21146041,-0.13568023], [ 0.        , 0.        , 0.        , 1.        ]]'

Or use a regular expression to remove all whitespaces:

>>> import re
>>> re.sub(r'\s+', '', np.array2string(arr, threshold=np.inf, max_line_width=np.inf, separator=','))
'[[0.15555605,0.51031528,0.84580176,0.06722675],[0.60556045,0.62721023,-0.48979983,-0.04152777],[-0.78044785,0.58837543,-0.21146041,-0.13568023],[0.,0.,0.,1.]]'

Agreed these aren't really "short" and they are also slower than converting to a list with .tolist() and then to a string but it's probably a good alternative especially if you want to customize the printed result without creating a (potentially huge) unnecessary list.

1
  • To add to this, If you have a list object and want to do the same, just wrap it in np.array() Sep 12, 2019 at 11:47

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.