1

I want to print 2D array without brackets and so that elements are aligned. I went through bunch of posts and manged to remove brackets but then elements are not aligned anymore and if I print my array like it is, elements are aligned but then there are brackets.

My code is simple

a = np.asarray(b._BaseArray__data).reshape(3,3)
print(a)

Where b._BaseArray__data represents list.

So how can I print my array that will be aligned and without brackets and commas?

Current result:

[[1, 2, 3, 4]
 [5, 6, 7, 8]
 [9,10,11,12]]

And I want it like this:

1  2  3  4
5  6  7  8
9 10 11 12
4
  • You can just use the docs for formatting, or provide a separator argument to print and loop through the array Commented Oct 20, 2018 at 10:07
  • How does your data look like? How should the output look like? You're getting down-votes because you do not provide all the necessary information for us to help you. Please add a working, minimal example to your question! Commented Oct 20, 2018 at 11:08
  • 1
    There I added current and wanted result Commented Oct 20, 2018 at 11:12
  • That sounds like a lot more work than it's worth. How flexible do you want to be in alignment? Just handling this simple example, or something more general? Commented Oct 20, 2018 at 14:49

1 Answer 1

1

The following code should do what you want with two nested forloops:

import numpy as np

# example data
arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])


def print_array(arr):
    """
    prints a 2-D numpy array in a nicer format
    """
    for a in arr:
        for elem in a:
            print("{}".format(elem).rjust(3), end="")
        print(end="\n")


# call the printing function
print_array(arr)

prints:

  1  2  3  4
  5  6  7  8
  9 10 11 12
Sign up to request clarification or add additional context in comments.

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.