Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Creating a matrix?

Reply
Thread Tools

Creating a matrix?

 
 
Ivan Voras
Guest
Posts: n/a
 
      04-02-2004
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
 
Reply With Quote
 
 
 
 
Peter Maas
Guest
Posts: n/a
 
      04-02-2004
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 http://www.velocityreviews.com/forums/(E-Mail Removed)
-------------------------------------------------------------------
 
Reply With Quote
 
 
 
 
Mike C. Fletcher
Guest
Posts: n/a
 
      04-02-2004
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/



 
Reply With Quote
 
Ivan Voras
Guest
Posts: n/a
 
      04-02-2004
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 )


 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      04-02-2004
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
 
Reply With Quote
 
Peter Maas
Guest
Posts: n/a
 
      04-02-2004
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 (E-Mail Removed)
-------------------------------------------------------------------
 
Reply With Quote
 
Josiah Carlson
Guest
Posts: n/a
 
      04-02-2004
> [ [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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Error Creating Control" when creating a custom control (Design Time). Can't see tooltip message. Ravi Ambros Wallau ASP .Net Web Controls 0 06-01-2005 02:36 PM
"Error Creating Control" when creating a custom control (Design Time). Can't see tooltip message. Ravi Ambros Wallau ASP .Net 0 06-01-2005 02:36 PM
Re: Creating a network with a single wireless computer Pavel A. Wireless Networking 1 08-03-2004 12:19 AM
Re: Creating a network with a single wireless computer Tim Levy Wireless Networking 0 08-02-2004 07:32 PM
creating Network Traffic Wireless Networking 1 07-06-2004 03:52 PM



Advertisments