0

Convert PHP code:

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

To python:

self.plane = {};

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

Does not work.

1) self.plane[slice_id][f] -> not yet initialized. Only create this element if necessary.

2) [] -> push element to array

3) Python dictionary are not usually multi-dimensional

ProfileTwist
  • 1,524
  • 1
  • 13
  • 18
  • In Python the closest to PHP's array is an [infinite defaultdict](http://stackoverflow.com/questions/4178249/infinitely-nested-dictionary-in-python). – Ashwini Chaudhary Dec 06 '14 at 20:10

1 Answers1

0

Try like this

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

The method append() appends a passed obj into the existing list.

self.plane[slice_id][f][] = {'x' : y, 'y' : z}; is not valid syntax for the python

Demo

>>> new_list = []
>>> new_list[] = 'test'
  File "<stdin>", line 1
    new_list[] = 'test'
             ^
SyntaxError: invalid syntax
Adem Öztaş
  • 20,457
  • 4
  • 34
  • 42