# tensor.py - Version 2.1, Sept 25 2006 by leonardo maffi from types import NoneType # for tensor from copy import deepcopy # for tensor def tensor(sizes=0, elem=0): """tensor(sizes=0, elem=0): creates a list of lists of lists... The parameter sizes can be a number or a tuple/list or sizes. >>> tensor() [] >>> tensor(()) [] It works with a single number or a sequence of numbers: >>> tensor(3) [0, 0, 0] >>> tensor((3,)) [0, 0, 0] >>> tensor((3), None) [None, None, None] >>> tensor((2, 3)) # array of 2 rows and 3 colums. [[0, 0, 0], [0, 0, 0]] >>> tensor((2, 3), 2) [[2, 2, 2], [2, 2, 2]] >>> tensor((1, 2, 3)) [[[0, 0, 0], [0, 0, 0]]] >>> tensor((2, 2, 3)) [[[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]]] It works with mutables too, calling deepcopy: >>> r = tensor((2, 3), [3]) >>> r [[[3], [3], [3]], [[3], [3], [3]]] >>> r[0][2][0] = 0 >>> r [[[3], [3], [0]], [[3], [3], [3]]] """ if isinstance(sizes, (int, long)): sizes = [sizes] elif not isinstance(sizes, list): sizes = list(sizes) result = [] if sizes: first = sizes.pop() if isinstance(elem, (int, long, basestring, tuple, NoneType, bool)): result = [elem] * first else: result = [deepcopy(elem) for i in xrange(first)] while sizes: result = [deepcopy(result) for i in xrange(sizes.pop())] return result if __name__ == "__main__": import doctest doctest.testmod() print "Tests finished."