insert a number before each number in the field according to iterations. Python

I need to insert a number before each number in the field according to the iterations. Something like a counter

my input

[[1 2 3]
 [1 2 3]
 [1 2 3]]

This is my output

1 1 1
2 2 2
3 3 3
4 4 4...
...

I need

11 21 31
12 22 32
13 23 33
14 24 34...
...

This is my code

    c1=np.array([w])
    c1 = [int(i) for i in c1[0].replace(" ", "").split(",")]
    c1=np.array([c1]*3)
    c1=np.transpose(c1)

It is possible?

Upvotes: 0

Views: 68

Answers (2)

Samuel
Samuel

Reputation: 220

Using Martix operation,

import numpy as np
c1=np.array([[1,1,1], [2,2,2], [3,3,3], [4,4,4]])
c1+10*np.ones((4,1))*((1,2,3))

result

array([[11., 21., 31.],
       [12., 22., 32.],
       [13., 23., 33.],
       [14., 24., 34.]])

Upvotes: 0

Szala
Szala

Reputation: 191

This might not be what you want, but you can achieve a similar effect with:

import numpy as np
result = np.fromfunction(lambda i, j: (j+1)*10 + (i+1), (4, 3), dtype=int)
print(result)

Output:

[[11 21 31]
 [12 22 32]
 [13 23 33]
 [14 24 34]]

And you can modify the function according to your input, however you want it.

Upvotes: 1

Related Questions