0

I am trying to code a floorplan with Python in rhino. I started out with a very simple task of separating the plan into 'mass' and 'space'. I am trying to generate an array in an array which is representative of a grid. Now, my problem is that I get a weird output in the following code:

import rhinoscriptsyntax as rs
import Rhino.Geometry as rg
import scriptcontext
import System.Drawing as sd

grid_plane = [[0,0,0], [0,0,0], [0,0,0]]

grid_plane[0][0] = 'mass'

print grid_plane

grid_x = []
grid_y_and_x = []

for d in range(3):
grid_x.append(0)

print grid_x

for d in range(3):
grid_y_and_x.append(grid_x)

print grid_y_and_x

grid_y_and_x[0][0] = 'mass'

print grid_y_and_x

The outputs I get are as follows:

[['mass', 0, 0], [0, 0, 0], [0, 0, 0]]
[0, 0, 0]
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
[['mass', 0, 0], ['mass', 0, 0], ['mass', 0, 0]]

I do not understand why mass is added in every list as I only want to add it in the fist list at the first instance.

I was expecting the output to be: [['mass', 0, 0], [0, 0, 0], [0, 0, 0]] for the second part.

but it is: [['mass', 0, 0], ['mass', 0, 0], ['mass', 0, 0]]

khelwood
  • 55,782
  • 14
  • 81
  • 108
  • You added the list `grid_x` into your `grid_y_and_x` three times. So instead of containing three lists, `grid_y_and_x` contains one list three times. – khelwood May 24 '23 at 08:23
  • @khelwood thanks for the answer, that makes sense. I thought because I am only changing the list afterwards it is not a problem...but apparently it is. Do you know how I could fix this easily or do I need a completely other approach to generatin the Matrix? – buklojn May 24 '23 at 10:00
  • See [the linked duplicate](https://stackoverflow.com/questions/240178/list-of-lists-changes-reflected-across-sublists-unexpectedly) for ways to create nested lists. – khelwood May 24 '23 at 10:22

0 Answers0