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

  for i in range(size - 1):      
    l += [0]
  x = l[:]
  for i in range(size):
    y += [x[:]]
  for i in range(size):
    z += [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[i][u][v] = 1




print(z)

i posted a similar question to this and now I'm trying to figure out why there is a repeat instance of 1 still. [[[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]], [[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]] this is my output. there should be one 1 not 7. I know where the issue is i know what the issue is just not why it is or how to fix it from here. I dont even care about fixing it as much as i do knowing why what im trying isnt working.

2 Answers2

0

As noted in comments, it's a reference copy somewhere. Since you have more than 2 levels, you cannot possibly clone all references just by abusing the use of [:].

The fastest & most pythonic way to achieve a proper structure creation is to drop all your loops & copies (which probably crawl, even if they worked, using copy.deepcopy() for instance) and use a nested list comprehension:

 z = [[[0]*size for _ in range(size)] for _ in range(size)]

The inner level can use [0]*size because integers are immutable, so no need for [0 for _ in range(size)] as we sometimes see.

So, one line, clear, and fast. Use it.

Jean-François Fabre
  • 126,787
  • 22
  • 103
  • 165
0

The issue you are having is due to the construction of your list. Since you create lists by adding them to other list there are multiple instances where an individual list points to another part of your list. As a result, when you edit one part of the list it changes the others that point to it.

I was able to figure this out by duplicating your code and manually changing z[1][1][1]. When I altered this spot in the list it changed the entire list.

Solution:

I recommend either using the above commenters code or stop repeatedly adding list to list.