[英]How to make a string-based multi-dimensional array in Python
我正在練習一些Python,並且在嘗試將我以前對多維數組的PHP理解應用於Python數組時遇到錯誤。
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" = []
]
]
這是我要設置的數組,具有多個分辨率的數組可以根據傳遞給迭代器的一組指令進行迭代以移至下一個區域。
我試圖執行我的代碼,並遇到以下錯誤(試圖查看語法是否合法):
文件“ menu.py”,第159行“是:正在傳入..”:[^ SyntaxError:語法無效
我嘗試將=
符號更改為:
, ==
(比較,可以正常工作,但沒有達到我的預期...) , 但什么也沒做。
我打算這樣做的是在數組的第一層進行迭代,如下所示:
for instruction, resolution in maze_path:
#// do some stuff with each of these informatants
manage( instruction, resolution, maze_path )
然后,我將找出與遍歷數組有關的其他問題。
主要問題 :我可以在Python中制作基於字符串的多維數組嗎?
使用dict
結構。 您可以使用.items()
訪問一組( key
, value
)元組,並在這些對之間進行迭代:
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)
輸出:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.