just a student - 2 years ago 83

Python Question

say I have array1 and it equals

`array1=np.zeros((3,3))`

that means [[0 0 0]

[0 0 0]

[0 0 0]]

but if I try the following it outputs an error:

`array2=np.array[[111,222,333],[444,555,666],[77`

array1[1,1]=array2

so how can I

for example

`for i in range(3):`

for j in range (3):

if i-j==0:

array1[i,j]=array2

so it will become 3*9 instead of 3*3 ?

`[[[251, 123, 584],`

[251, 123, 584],

[251, 123, 584]],

[[251, 123, 584],

[251, 123, 584],

[251, 123, 584]],

[[251, 123, 584],

[251, 123, 584],

[251, 123, 584]]]

Recommended for you: Get network issues from **WhatsUp Gold**. **Not end users.**

Answer Source

You cannot change the size (number of elements) of a numpy array. But you could use lists as intermediate step to create that final array:

```
>>> import numpy as np
>>> array1 = np.zeros((3,3))
>>> array2 = [251,123,584]
>>> np.array([[array2 for _ in row] for row in array1.tolist()])
array([[[251, 123, 584],
[251, 123, 584],
[251, 123, 584]],
[[251, 123, 584],
[251, 123, 584],
[251, 123, 584]],
[[251, 123, 584],
[251, 123, 584],
[251, 123, 584]]])
```

With some intermediate steps:

```
# Convert the original array to a list
>>> array1.tolist()
[[1.0, 2.0, 3.0], [1.0, 2.0, 3.0], [1.0, 2.0, 3.0]]
# Iterate over all elements and replace the element by array2:
>>> [[array2 for _ in row] for row in array1.tolist()]
[[[251, 123, 584], [251, 123, 584], [251, 123, 584]],
[[251, 123, 584], [251, 123, 584], [251, 123, 584]],
[[251, 123, 584], [251, 123, 584], [251, 123, 584]]]
# Convert the list to a numpy array again
>>> np.array([[array2 for _ in row] for row in array1.tolist()])
...
```

Recommended from our users: **Dynamic Network Monitoring from WhatsUp Gold from IPSwitch**. ** Free Download**