-1

I'm working on practising some Python, and I've encountered an error from trying to apply my previous PHP understanding of multidimensional arrays into Python's arrays.

maze_path = [
    [
        "You are in a forest looking into the shrubbery while sitting on a plane. Do you decide to check it out?",
        "yes:Walking in.." = [

        ],
        "no:END_GAME" = []
    ]
]

This is the array that I'm trying to setup, an array with more than one resolutions that can be iterated through to move to the next areas depending on a set of instructions that are given to the iterator.

I tried to execute my code and ran into the following error (was trying to see if syntax was legitimate):

File "menu.py", line 159 "yes:Walking in..": [ ^ SyntaxError: invalid syntax

I tried changing the = sign into :, == (comparison, worked but not as I expected...) and just nothing with it.

What I am planning on doing with this is iterating on the first level of the array, something like so:

for instruction, resolution in maze_path:
  #// do some stuff with each of these informatants
  manage( instruction, resolution, maze_path )

Then from that I'll figure out some other issue to do with moving through the array.

The main question: Am I able to make string-based multidimensonal arrays in Python?

4
  • 4
    If you want an associative array in Python, that's dict. Commented May 11, 2018 at 4:56
  • The errror message is not about multidimensionality or arrays or dicts; it's about an assignment you make in the middle of an array constant. And addionally, this assignment has a string constant as ist LH-value - so in short: what are you trying to do there, Independent from the programming language? Commented May 11, 2018 at 5:08
  • Related stackoverflow.com/questions/1781617/… Commented May 11, 2018 at 5:08
  • 2
    PHP arrays are not like Python list objects. They are sort of a hybrid between Python list and dict objects. Among Pythonistas, "array" usually means numpy.array, which is a true multidimensional array data-structure. It can also mean the built-in array.array, but you shouldn't refer to list objects, e.g. [1,2,3] nor dict objects, e.g. {'a':1, 'b':2} as "arrays" Commented May 11, 2018 at 6:19

1 Answer 1

2

Use the dict structure. You can access a set of (key,value) tuples with .items(), and iterate over those pairs:

maze_path = {
    "You are in a forest looking into the shrubbery while sitting on a plane. Do you decide to check it out?":
    {"yes": "Walking in..",
     "no": "END_GAME" 
    }
}

def manage(i, r):
    print("     You chose {}, this happens: {}".format(i, r))

for description, options in maze_path.items():
    print(description)
    for instruction, resolution in options.items():
        manage(instruction, resolution)

Output:

You are in a forest looking into the shrubbery while sitting on a plane. Do you decide to check it out?
     You chose yes, this happens: Walking in..
     You chose no, this happens: END_GAME
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.