RE: proposition for syntax for initialisation of multidimensionallists

Discussion in 'Python' started by Batista, Facundo, Oct 12, 2004.

  1. [Nils Grimsmo]

    #- 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]]]

    You don't need list comprehensions::

    >>> [[[0] * 4] * 3] * 2

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

    .. Facundo
     
    Batista, Facundo, Oct 12, 2004
    #1
    1. Advertising

  2. "Batista, Facundo" wrote:

    > You don't need list comprehensions::
    >
    > >>> [[[0] * 4] * 3] * 2

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


    Yes, you do:

    >>> a = [[0] * 3] * 2
    >>> a[1][2] = 100
    >>> a

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

    --
    __ Erik Max Francis && && http://www.alcyone.com/max/
    / \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
    \__/ I never think of the future. It comes soon enough.
    -- Albert Einstein
     
    Erik Max Francis, Oct 12, 2004
    #2
    1. Advertising

  3. Batista, Facundo

    Russell Blau Guest

    "Batista, Facundo" <> wrote in message
    news:...
    >
    > You don't need list comprehensions::
    >
    > >>> [[[0] * 4] * 3] * 2

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


    Lists are mutable... ;-)

    >>> x = [[[0] * 4] * 3] * 2
    >>> x[0][1][2] = "foo!"
    >>> x

    [[[0, 0, 'foo!', 0], [0, 0, 'foo!', 0], [0, 0, 'foo!', 0]], [[0, 0, 'foo!',
    0], [0, 0, 'foo!', 0], [0, 0, 'foo!', 0]]]

    --
    I don't actually read my hotmail account, but you can replace hotmail with
    excite if you really want to reach me.
     
    Russell Blau, Oct 12, 2004
    #3
    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. Simon

    Proposition

    Simon, Nov 6, 2003, in forum: XML
    Replies:
    2
    Views:
    466
    Tad McClellan
    Nov 6, 2003
  2. Nils Grimsmo
    Replies:
    1
    Views:
    269
    Michael Hoffman
    Oct 12, 2004
  3. Tim Clacy
    Replies:
    8
    Views:
    396
    Tim Clacy
    May 30, 2006
  4. Dirk Meijer

    method proposition

    Dirk Meijer, Jan 21, 2006, in forum: Ruby
    Replies:
    11
    Views:
    156
  5. Michael Hrivnak

    Re: Is Python a commercial proposition ?

    Michael Hrivnak, Jul 29, 2012, in forum: Python
    Replies:
    2
    Views:
    193
    Thomas 'PointedEars' Lahn
    Jul 29, 2012
Loading...

Share This Page