0
def create_octahedron(size):
    x = []
    y = []
    z = []
    if size % 2 == 0 or size <= 1:
        return x

    for i in range(size):
        x.append(0)
    for i in range(size):
        y.append(x)
    for i in range(size):
        z.append(y)

    for i in range(size):

        for u in range(size):

            for v in range(size):

              if i == len(z)//2:
                if u == len(y)//2:
                  if v == len(x)//2:
                    z[3][3][3] = 1

    print(z)


create_octahedron(7)

[[[0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0]], [[0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0]], [[0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0]], [[0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0]], [[0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0]], [[0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0]], [[0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0]]]

this is the output i keep getting but the output I'm expecting is to only have a 1 at the middle of the entire equation not at. i am much less interested in how to fix this as i already know how. What i want to know is why this is giving this output.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • If you already know how to fix this, then by definition, you know why it giving this output. – Richardissimo Dec 01 '18 at 06:03
  • Because you append the same array. In z, each row points the same Y, and in Y each row points to the same X. If you try z[0][0][0] = 2, you could see that every row's first element changes to 2. – Frank Dec 01 '18 at 06:03
  • You have the same problem as the duplicate question, though you arrived at that situation a little differently (by repeatedly appending references to the same lists, rather than using the `*` operator). – Blckknght Dec 01 '18 at 06:07
  • haha thats like saying because I can trouble shoot a computer than I understand the issue i was facing to begin with. I want to understand the issue not fix it. –  Dec 01 '18 at 06:14

1 Answers1

0

Because you append the same list. In z, each row points the same Y, and in Y each row points to the same X. If you try z[0][0][0] = 2, you could see that every row's first element changes to 2.

To avoid this, create a new x/y list before append.

Frank
  • 1,215
  • 12
  • 24
  • update: if i change the x's in z[x][x][3] the output will always be the same. for z[1][2][3] i get the same output as what i have currently (the original was z[i][u][v] = 1 but i changed it for clarity) –  Dec 01 '18 at 06:17
  • Yes no matter which x or y you change in z, they point to the same list. So z[1][2] and z[0][0] point to the same list. – Frank Dec 02 '18 at 21:57
  • If you print their id by `print id(z[0][0])` and `print id(z[1][2])` you could see that they have the same id. So they are the same list. – Frank Dec 02 '18 at 22:04