0
對Python來說很新,並且無法根據變量名來檢查(或查找)如何檢查數組。從Python中的變量名獲取數組
如:
type = "world"
world_arr = []
town_arr = []
#add to an array
[type + '_arr'].append('test') <-- how?
對Python來說很新,並且無法根據變量名來檢查(或查找)如何檢查數組。從Python中的變量名獲取數組
如:
type = "world"
world_arr = []
town_arr = []
#add to an array
[type + '_arr'].append('test') <-- how?
別看了變量名動態。這是一個主要的代碼氣味。使用字典。
lists = {
'world': [],
'town': []
}
type = 'world'
lists[type].append('test')
'全局()[式+ '_arr']。追加( '試驗')'?如果數組是全局/模塊級變量 –
我不太喜歡[鏈接重複問題](https://stackoverflow.com/questions/9437726/how-to-get-the-value-of-a-variable -given-其名-IN-A-字符串)。按名稱查找變量幾乎總是一個壞主意。問怎麼做是一個經典的[XY問題](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)。 –