2 dimensional array insertion in php python equivalent -
i have little experience in php , have convert php script python. couldn't understand these lines in code:
$vars = array(); $vars['a'] = array(); $vars['b'] = array(); $vars['b'][] = 'text1'; what last line stand for? , happen if add line below code?
$vars['b'][] = 'text2'; i appreciate on converting python. lot,
if convert php code snippet python, closest can
>>> var = {} >>> var['a'] = {} >>> var['b'] = {} >>> var['b'][len(var['b'] )] = 'text1' >>> var['b'][len(var['b'] )] = 'text2' >>> var {'a': {}, 'b': {0: 'text1', 1: 'text2'}} another variation
>>> class array(dict): def __getitem__(self, key): return dict.__getitem__(self, key) def __setitem__(self, key, value): if key == slice(none, none, none): dict.__setitem__(self, self.__len__(), value) else: dict.__setitem__(self, key, value) >>> var = array() >>> var['a'] = array() >>> var['b'] = array() >>> var['b'][:] = 'text1' >>> var['b'][:] = 'text2' >>> var {'a': {}, 'b': {0: 'text1', 1: 'text2'}}
Comments
Post a Comment