faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 4 people (75%) answered Yes
Recently 2 of 3 people (67%) answered Yes

Entry

Tensors

Jul 5th, 2000 10:03
Nathan Wallace, Hans Nowak, Snippet 306, Stephan Houben


"""
Packages: basic_datatypes.lists
"""
"""
> Most definitely a feature.  You're getting the same reference for all
> four list elements.  To break it apart:
Nevertheless, it makes it difficult to construct multi-dimensional
"arrays".
For this, I would like to propose the following function which I use: (why
isn't something like this in the standard library, or didn't I just look
well enough?)
"""
# Make a tensor (i.e. list of list of lists...)
def make_tensor(*args):
    if args:
        head = args[0]
        tail = args[1:]
        result = []
        for i in range(head):
            result.append(apply(make_tensor, tail))
        return result
    else:
        return None
if __name__ == '__main__':
    print make_tensor()
    print make_tensor(3)
    print make_tensor(1,2)