-1

How can I print two equal size arrays aligned by rows?

For example:

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

rv = np.array([["R1x ="],
               ["R1y ="],
               ["R5x ="],
               ["R5y ="]])

print(rv, a)

I want that the code prints the code below:

R1x = 1
R1y = 2
R5x = 3
R5y = 4

1 Answers1

1

Use zip for that:

for val, string in zip(a.flatten(), rv.flatten()):
    print(f"{string} {val}")

# out:
R1x = 1
R1y = 2
R5x = 3
R5y = 4
mcsoini
  • 6,280
  • 2
  • 15
  • 38
  • Thank you so much, it worked on this code! However in a more complex code that I have the output was "R1x = 1 2 3 4". –  Mar 12 '22 at 10:48
  • THen the structure of the array is likely different. In the end, you always need to pass 2 1D lists/iterables to zip. – mcsoini Mar 12 '22 at 10:49