22

I tested PyCharm and IDLE, both of them print the 7th number to a second line.

Input:

import numpy as np
a=np.array([ 1.02090721,  1.02763091,  1.03899317,  1.00630297,  1.00127454, 0.89916715,  1.04486896])
print(a)

Output:

[ 1.02090721  1.02763091  1.03899317  1.00630297  1.00127454  0.89916715
  1.04486896]

How can I print them in one line?

6
  • This only happens because your IDE is line wrapping the console. Commented Mar 22, 2018 at 20:25
  • here's a duplicate post with an answer stackoverflow.com/questions/44139508/… Commented Mar 22, 2018 at 20:25
  • it is not duplicate, in my case, the dimension is 1x7 Commented Mar 22, 2018 at 20:27
  • 1
    @alessandrocb, I know wrapping, but my case is not wrapping, it is just printing the 7th to a second line. Commented Mar 22, 2018 at 20:28
  • @zerocool That question explicitly set linewidth to 1000 and it didn't help because it wasn't a 1D array. For this question, the answer is just to set linewidth. (Plus, that questioner wanted the list equivalent, this one doesn't.) Commented Mar 22, 2018 at 20:30

4 Answers 4

25

There is np.set_printoptions which allows to modify the "line-width" of the printed NumPy array:

>>> import numpy as np

>>> np.set_printoptions(linewidth=np.inf)
>>> a = np.array([ 1.02090721,  1.02763091,  1.03899317,  1.00630297,  1.00127454, 0.89916715,  1.04486896])
>>> print(a)
[1.02090721 1.02763091 1.03899317 1.00630297 1.00127454 0.89916715 1.04486896]

It will print all 1D arrays in one line. It won't work that easily with multidimensional arrays.


Similar to here you could use a contextmanager if you just want to temporarily change that:

import numpy as np
from contextlib import contextmanager

@contextmanager
def print_array_on_one_line():
    oldoptions = np.get_printoptions()
    np.set_printoptions(linewidth=np.inf)
    yield
    np.set_printoptions(**oldoptions)

Then you use it like this (fresh interpreter session assumed):

>>> import numpy as np
>>> np.random.random(10)  # default
[0.12854047 0.35702647 0.61189795 0.43945279 0.04606867 0.83215714
 0.4274313  0.6213961  0.29540808 0.13134124]

>>> with print_array_on_one_line():  # in this block it will be in one line
...     print(np.random.random(10))
[0.86671089 0.68990916 0.97760075 0.51284228 0.86199111 0.90252942 0.0689861  0.18049253 0.78477971 0.85592009]

>>> np.random.random(10)  # reset
[0.65625313 0.58415921 0.17207238 0.12483019 0.59113892 0.19527236
 0.20263972 0.30875768 0.50692189 0.02021453]
Sign up to request clarification or add additional context in comments.

Comments

11

If you want a customized version of str(a), the answer is array_str:

>>> print(a)
[ 1.02090721  1.02763091  1.03899317  1.00630297  1.00127454  0.89916715
  1.04486896]
>>> str(a)
'[1.02090721 1.02763091 1.03899317 1.00630297 1.00127454 0.89916715\n 1.04486896]'
>>> np.array_str(a, max_line_width=np.inf)
'[1.02090721 1.02763091 1.03899317 1.00630297 1.00127454 0.89916715 1.04486896]'
>>> print(np.array_str(a, max_line_width=np.inf)
[1.02090721 1.02763091 1.03899317 1.00630297 1.00127454 0.89916715 1.04486896]

If you want to change the printout of every array, not just here, see set_printoptions.

2 Comments

Or the generalized version: array2string. But it's by default a bit closer to the repr.
ofc it doesnt work for multi dimensional array, because why would it ? I sure downloaded numpy just to use it on 1D array, the fact that a simple print function doesnt work speaks volumes on the quality of this lib
4

Type cast to a list when printing.

import numpy as np
a=np.array([ 1.02090721,  1.02763091,  1.03899317,  1.00630297,  1.00127454, 0.89916715,  1.04486896])
print(list(a))

This will print on a single line.

Comments

1

list comprehension is a clean, simple approach that works for lists, tuples, or numpy arrays.

import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(', '.join(format(k) for k in arr))

produces simple output without brackets:

1, 2, 3, 4, 5

then this:

print(', '.join(format(k, '8.3f') for k in arr))

produces formatted output without brackets and leading spaces or trailing decimal places with the format() statement:

   1.000,    2.000,    3.000,    4.000,    5.000

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.