2014-12-06 55 views
0

轉換PHP代碼:的Python:追加解釋新元素

$this->plane[$slice_id][$f][] = array('x' => $y, 'y' => $z);

到Python:

self.plane = {};

self.plane[slice_id][f][] = {'x' : y, 'y' : z};

不工作。

1)self.plane[slice_id][f] - >尚未初始化。只有在必要時才創建此元素。

2)[] - >推動元件以陣列

3)Python字典通常不是多維

+0

在Python最接近PHP的陣列是[無限defaultdict](http://stackoverflow.com/questions/4178249 /無限嵌套詞典功能於蟒)。 – 2014-12-06 20:10:51

回答

0

嘗試這樣

self.plane[slice_id][f] = [] 
self.plane[slice_id][f].append({'x' : y, 'y' : z}) 

方法append()追加傳遞obj入現有列表。

self.plane[slice_id][f][] = {'x' : y, 'y' : z};不是蟒有效語法

演示

>>> new_list = [] 
>>> new_list[] = 'test' 
    File "<stdin>", line 1 
    new_list[] = 'test' 
      ^
SyntaxError: invalid syntax