Creating a matrix?

Discussion in 'Python' started by Ivan Voras, Apr 2, 2004.

  1. Ivan Voras

    Ivan Voras Guest

    Is there a nice(r) way of creating a list of uniform values? I'm currently
    using: list('0'*N), which makes a string and then chops it up into a list. I
    need it to create a NxN matrix:

    matrix = [list('0'*N) for i in range(N)]

    (elements need to be mutable afterwards, a shallow copy is bad)

    While this is short and concise, it also feels odd :)
     
    Ivan Voras, Apr 2, 2004
    #1
    1. Advertising

  2. Ivan Voras

    Peter Maas Guest

    Ivan Voras wrote:
    > Is there a nice(r) way of creating a list of uniform values? I'm
    > currently using: list('0'*N), which makes a string and then chops it up
    > into a list. I need it to create a NxN matrix:
    >
    > matrix = [list('0'*N) for i in range(N)]


    matrix = [['0']*N]*N

    Mit freundlichen Gruessen,

    Peter Maas

    --
    -------------------------------------------------------------------
    Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
    Tel +49-241-93878-0 Fax +49-241-93878-20 eMail
    -------------------------------------------------------------------
     
    Peter Maas, Apr 2, 2004
    #2
    1. Advertising

  3. N*N matrix of lists...

    [ [0]*N for i in range( N ) ]

    Seems clearer to me, anyway (and stores actual 0 values, not '0' in the
    result). Have fun,
    Mike

    Ivan Voras wrote:

    > Is there a nice(r) way of creating a list of uniform values? I'm
    > currently using: list('0'*N), which makes a string and then chops it
    > up into a list. I need it to create a NxN matrix:
    >
    > matrix = [list('0'*N) for i in range(N)]
    >
    > (elements need to be mutable afterwards, a shallow copy is bad)
    >
    > While this is short and concise, it also feels odd :)


    _______________________________________
    Mike C. Fletcher
    Designer, VR Plumber, Coder
    http://members.rogers.com/mcfletch/
     
    Mike C. Fletcher, Apr 2, 2004
    #3
  4. Ivan Voras

    Ivan Voras Guest

    Peter Maas wrote:
    > Ivan Voras wrote:
    >
    >> Is there a nice(r) way of creating a list of uniform values? I'm
    >> currently using: list('0'*N), which makes a string and then chops it
    >> up into a list. I need it to create a NxN matrix:
    >>
    > > matrix = [list('0'*N) for i in range(N)]

    >
    > matrix = [['0']*N]*N


    I can't believe I didn't try that one :)

    (hmm, actually, I think I tried it but the results were bad for some
    reason... oh well, thank you :) )
     
    Ivan Voras, Apr 2, 2004
    #4
  5. Ivan Voras

    Peter Hansen Guest

    Peter Maas wrote:

    > Ivan Voras wrote:
    >
    >> Is there a nice(r) way of creating a list of uniform values? I'm
    >> currently using: list('0'*N), which makes a string and then chops it
    >> up into a list. I need it to create a NxN matrix:
    >>
    > > matrix = [list('0'*N) for i in range(N)]

    >
    > matrix = [['0']*N]*N


    Sorry, Peter, but that's a rookie mistake:

    >>> matrix = [['0'] * 4] * 4
    >>> matrix

    [['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0', '0', '0', '0'], ['0',
    '0', '0
    ', '0']]
    >>> matrix[1][2] = '1'
    >>> matrix

    [['0', '0', '1', '0'], ['0', '0', '1', '0'], ['0', '0', '1', '0'], ['0',
    '0', '1
    ', '0']]

    Notice that each of the inner lists contains four seperate references
    to the string '0', but the outer repetition simply copies the inner
    list four times, resulting in four references to the same inner list!
    Changing any one element affects the same entry in each of the other
    three lists...

    -Peter
     
    Peter Hansen, Apr 2, 2004
    #5
  6. Ivan Voras

    Peter Maas Guest

    Peter Hansen wrote:
    > >>> matrix[1][2] = '1'
    > >>> matrix

    > [['0', '0', '1', '0'], ['0', '0', '1', '0'], ['0', '0', '1', '0'], ['0',
    > '0', '1
    > ', '0']]
    >
    > Notice that each of the inner lists contains four seperate references
    > to the string '0', but the outer repetition simply copies the inner
    > list four times, resulting in four references to the same inner list!
    > Changing any one element affects the same entry in each of the other
    > three lists...


    Sorry:

    matrix = [['0']*N for i in range(N)]

    Mit freundlichen Gruessen,

    Peter Maas

    --
    -------------------------------------------------------------------
    Peter Maas, M+R Infosysteme, D-52070 Aachen, Hubert-Wienen-Str. 24
    Tel +49-241-93878-0 Fax +49-241-93878-20 eMail
    -------------------------------------------------------------------
     
    Peter Maas, Apr 2, 2004
    #6
  7. > [ [0]*N for i in range( N ) ]

    xrange is a bit faster for large N, you don't need to create a list that
    gets destroyed.

    - Josiah
     
    Josiah Carlson, Apr 2, 2004
    #7
    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. Ravi Ambros Wallau
    Replies:
    0
    Views:
    4,104
    Ravi Ambros Wallau
    Jun 1, 2005
  2. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,687
    Jonathan Bromley
    Jul 5, 2006
  3. Holgerson

    Matrix*Vector and Vector*Matrix

    Holgerson, Oct 25, 2007, in forum: C++
    Replies:
    3
    Views:
    422
    Holgerson
    Oct 26, 2007
  4. Terry Reedy
    Replies:
    0
    Views:
    561
    Terry Reedy
    Apr 2, 2009
  5. Robert Kern
    Replies:
    0
    Views:
    606
    Robert Kern
    Apr 2, 2009
Loading...

Share This Page