According to this post
http://www.python-forum.org/pythonforum/viewtopic.php?f=3&t=31349#p146797
it claimed to be possible to create a list were you can store and recall data using a coordinate system ...

SomeList[1][5][2] = 234

I have been playing around with the code a while, and have ran into a problem.
No matter what I do, when I am assigning data to a coordinate, it ignores the first 2 coordinates, and writes the data to every slot for the third coordinate.

ListX=[]
ListY=[]
ListZ=[]
NewList3D=[]
DementionX=10
DementionY=5
DementionZ=5
for xx in range(DementionX):
    ListX.append([0])
for yy in range(DementionY):
    ListY.append(ListX)
for zz in range (DementionZ):
    ListZ.append(ListY)

NewList3D = ListZ    

NewList3D[4][4][9] = 123  #assign z y x
print(NewList3D[1][1][9]) #recall wrong z y x 
print("=="*20)
print(NewList3D)          #error

Any help trying to figure out whats wrong would be appreciated. Thank you.

Recommended Answers

All 2 Replies

Maybe this example helps. For this purpose I always use list comprehentions. Do not use n*[some_list] as it produces references to same list, not independent lists.

# pretty module from my code snippet in Daniweb:
# http://www.daniweb.com/software-development/python/code/345935
import pretty

def get_by_iter(data, iterable):
    # access data by single iterable instead of multiple []
    for t in iterable:
        data = data[t]
    return data


# using list of three dimensions
dimentions= 10, 5, 5
listXYZ = [[[x*y*z for x in range(dimentions[0])]
                for y in range(dimentions[1])]
                   for z in range(dimentions[2])]
listXYZ[1][2][3] = 123
print get_by_iter(listXYZ, (1,2,3))
pretty.printer(listXYZ)
'''Output:
123

  [
    [
      [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, 0, 0]], 
    [
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
      [0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 
      [0, 2, 4, 123, 8, 10, 12, 14, 16, 18], 
      [0, 3, 6, 9, 12, 15, 18, 21, 24, 27], 
      [0, 4, 8, 12, 16, 20, 24, 28, 32, 36]], 
    [
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
      [0, 2, 4, 6, 8, 10, 12, 14, 16, 18], 
      [0, 4, 8, 12, 16, 20, 24, 28, 32, 36], 
      [0, 6, 12, 18, 24, 30, 36, 42, 48, 54], 
      [0, 8, 16, 24, 32, 40, 48, 56, 64, 72]], 
    [
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
      [0, 3, 6, 9, 12, 15, 18, 21, 24, 27], 
      [0, 6, 12, 18, 24, 30, 36, 42, 48, 54], 
      [0, 9, 18, 27, 36, 45, 54, 63, 72, 81], 
      [0, 12, 24, 36, 48, 60, 72, 84, 96, 108]], 
    [
      [0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
      [0, 4, 8, 12, 16, 20, 24, 28, 32, 36], 
      [0, 8, 16, 24, 32, 40, 48, 56, 64, 72], 
      [0, 12, 24, 36, 48, 60, 72, 84, 96, 108], 
      [0, 16, 32, 48, 64, 80, 96, 112, 128, 144]]]
'''

Order when accessing is Z, Y, X (like you noticed) so better to call it list3d or listZYX rather than listXYZ.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.