1

I want to add elements to array print output. What I have done is like that(from 'Fallen Apart' my last post):

c = np.arange(9).reshape(3,3)
for i, row in enumerate(c):
    print('G' + str(i+1) + ': ' + str(row))

Result:

G1: [0 1 2]
G2: [3 4 5]
G3: [6 7 8]

What I want to do is to print like this:

G1: [1:0 2:1 3:2]
G2: [1:3 2:4 3:5]
G3: [1:6 2:7 3:8]

Anyone can help me with it? THanks!

3
  • 1
    what do you want to add? I can't make sense of [1:0 2:1 3:2], could you please clarify what you mean by that? Commented Jul 28, 2020 at 18:08
  • Just add 1:, 2: ,3:... in sequence before each value of each row in this array. Commented Jul 28, 2020 at 18:35
  • [1:0 2:1 3:2] is what I want Commented Jul 28, 2020 at 18:36

1 Answer 1

1

Similar to your code:

c = np.arange(9).reshape(3,3)
col_id = np.arange(c.shape[1])+1
for i, row in enumerate(c):
    print('G'+str(i+1)+': '+'[%s]'%' '.join([str(a)+':'+str(b) for a,b in zip(col_id,row)]))

or another equal solution:

c = np.arange(9).reshape(3,3)
col_id = np.arange(c.shape[1])+1
for i, row in enumerate(c):
    print('G'+str(i+1)+': '+'[%s]'%' '.join(["{}:{}".format(a,b) for a,b in zip(col_id,row)]))

output:

G1: [1:0 2:1 3:2]
G2: [1:3 2:4 3:5]
G3: [1:6 2:7 3:8]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help,sir! I try to adapt your code to mine.But it does not work to an array of 10*24,it will only output from 1:...to 10:, other values are ignored.How to solve this kind of problem?Many thanks!
I try to adapt it to mine(10*24).The output is like that ` G1 : [1:xx,2:xx,..10:xx (from 11 to 24 missed)] `

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.