0

I'm pretty new to Python and could use a hand in telling the computer what I want. I understand that

A = np.array([[3,5,50],[30,2,6],[25,1,1]] for i in A: print(i) 

gives the output:

[3,5,50] [30,2,6] [25,1,1] 

And, I understand that

for i,j,k in A: print(i,j,k) 

gives

3 5 50 30 2 6 25 1 1 

But, how would you write a loop to get the following output without using a print statement three times?

3 5 50 30 20 6 25 1 1 

(this is really general and I'm applying it to a large array and wanting to set specific values in the array equal to zero)

2 Answers 2

1

When using multiple arguments in print it uses the default separator which is a white space. You can specify it to be a new line:

for i,j,k in A: print(i,j,k, sep="\n") 

One way of doing so is using np.ravel() or np.flatten():

for i in A.ravel(): print(i) 
Sign up to request clarification or add additional context in comments.

Comments

1

Solution 1:

for i in A: for j in i: print(j) 

Solution 2:

for i in range(len(A)): for j in range(len(A[i])): print(A[i][j]) 

1 Comment

The A.ravel() sold me for the best answer to this question, but I want to try and implement this method in my code. Could you take a look at this question: stackoverflow.com/questions/60751580/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.