proposition for syntax for initialisation of multidimensional lists

Discussion in 'Python' started by Nils Grimsmo, Oct 12, 2004.

  1. Nils Grimsmo

    Nils Grimsmo Guest

    i always have trouble explaining why creation of multidimensional lists
    is not as straight forward as it could be in python. list comprehensions
    are ugly if you are new to the language. i really would like to see it
    made easy. i propose using tuples in the same way as integers are now.
    example:

    >>> [0] * (2,3,4)

    [[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0,
    0], [0, 0, 0, 0]]]

    this is even less "mathematically bad" syntax than multiplication with
    an integer, as multiplication of vectors is only well defined if they
    are of the same length. [1] * 7 could be interpreted as a vector of
    length one multiplied with a scalar 7 resulting in the vector [7]. (this
    was not meant as a proposition to remove the current syntax.)

    i would be easy to implement:

    class mylist(list):
    def __mul__(self, dims):
    if dims.__class__ == (()).__class__:
    if len(dims) > 1:
    return [self.__mul__(dims[1:]) for i in range(dims[0])]
    else:
    return list.__mul__(self, dims[0])
    else:
    return list.__mul__(self, dims)

    li = mylist()
    li.append(0)
    print li * (2,3,4)


    what do you think?


    klem fra nils
     
    Nils Grimsmo, Oct 12, 2004
    #1
    1. Advertising

  2. Re: proposition for syntax for initialisation of multidimensionallists

    Nils Grimsmo wrote:

    > i always have trouble explaining why creation of multidimensional lists
    > is not as straight forward as it could be in python.


    Maybe because that is not really a sensible thing to do with the list
    data structure, which can change in length after instantiation?

    > >>> [0] * (2,3,4)

    > [[[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]], [[0, 0, 0, 0], [0, 0, 0,
    > 0], [0, 0, 0, 0]]]
    >
    > [SNIP]
    >
    > what do you think?


    I think I will continue doing this:

    >>> import numarray
    >>> numarray.zeros((2,3,4))

    array([[[0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0]],

    [[0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0]]])
    --
    Michael Hoffman
     
    Michael Hoffman, Oct 12, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?q?Orla=20O'Sullivan?=

    multidimensional lists

    =?iso-8859-1?q?Orla=20O'Sullivan?=, Oct 7, 2003, in forum: Python
    Replies:
    1
    Views:
    309
    anton muhin
    Oct 7, 2003
  2. Batista, Facundo
    Replies:
    2
    Views:
    284
    Russell Blau
    Oct 12, 2004
  3. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    425
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  4. Tim Clacy
    Replies:
    8
    Views:
    406
    Tim Clacy
    May 30, 2006
  5. Adam Nielsen
    Replies:
    13
    Views:
    629
    Adam Nielsen
    Jan 20, 2009
Loading...

Share This Page