簡體   English   中英

Python:追加字典新元素

[英]Python: Append Dictionary new element

轉換PHP代碼:

$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字典通常不是多維的

這樣嘗試

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}; 是python的無效語法

演示版

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM