TransWikia.com

Can you store the position of a a multidimensional array in a variable?

Stack Overflow Asked on January 9, 2021

I am trying to write a rubix cube solver and I would like to call the same edge repeatedly to check information from it. However the position of the three dimensional area takes up a lot of space, I would like to put the positions that I use repeatedly into a variable so I can call the variable instead of rewriting the position over and over.

This is what my rubix cube looks like:

rubixCube = [
        [["G","G","Y"], ["R","B","O"], ["R","R","O"]],
        [["G","O","O"], ["O","Y","G"], ["R","B","B"]],
        [["Y","W","Y"], ["R","O","O"], ["O","W","W"]],
        [["B","B","W"], ["B","W","G"], ["G","W","R"]],
        [["B","Y","R"], ["Y","R","W"], ["G","Y","B"]], 
        [["O","G","W"], ["B","G","R"], ["Y","Y","W"]]
        ]

and this is an example of a position I call repeatly:

if(rubixCube[0][0][1] == "W"):

can I write something approximately looking like this:

position = [0][0][1]
if(rubixCube[position] == "W"):

2 Answers

If you are happy to install numpy, then this would be the standard way to handle multidimensional arrays.

If you want to keep within the standard library in order to avoid the need to install additional packages, you could make an indexer class to sit on top of your existing nested list, which will allow you to get and set elements using a tuple (or list) for the position.

class NdIndexer:
    def __init__(self, arr):
        self.arr = arr

    def __getitem__(self, index):
        val = self.arr
        for i in index:
            val = val[i]
        return val

    def __setitem__(self, index, value):
        val = self.arr
        for i in index[:-1]:
            val = val[i]
        val[index[-1]] = value

You can then do:

rubixCube = [
    [["G","G","Y"], ["R","B","O"], ["R","R","O"]],
    [["G","O","O"], ["O","Y","G"], ["R","B","B"]],
    [["Y","W","Y"], ["R","O","O"], ["O","W","W"]],
    [["B","B","W"], ["B","W","G"], ["G","W","R"]],
    [["B","Y","R"], ["Y","R","W"], ["G","Y","B"]], 
    [["O","G","W"], ["B","G","R"], ["Y","Y","W"]]
]

cube = NdIndexer(rubixCube)
position = (0,0,1)

# getting
print(rubixCube[0][0][1])  # prints: G
print(cube[position])  # prints: G

# setting
cube[position] = "X"

print(rubixCube[0][0][1])  # prints: X
print(cube[position])  # prints: X

Answered by alani on January 9, 2021

if you use a numpy array

import numpy as np

rubixCube = np.array([
        [["G","G","Y"], ["R","B","O"], ["R","R","O"]],
        [["G","O","O"], ["O","Y","G"], ["R","B","B"]],
        [["Y","W","Y"], ["R","O","O"], ["O","W","W"]],
        [["B","B","W"], ["B","W","G"], ["G","W","R"]],
        [["B","Y","R"], ["Y","R","W"], ["G","Y","B"]],
        [["O","G","W"], ["B","G","R"], ["Y","Y","W"]]
        ])

then you can index it with a tuple

pos = (0, 0, 1)
print(rubixCude[pos])

Answered by mazore on January 9, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP