473,322 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Creating a matrix?

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 :)
Jul 18 '05 #1
6 5632
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 pe********@mplusr.de
-------------------------------------------------------------------
Jul 18 '05 #2
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
Jul 18 '05 #3
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 :) )
Jul 18 '05 #4
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 pe********@mplusr.de
-------------------------------------------------------------------
Jul 18 '05 #5
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/

Jul 18 '05 #6
> [ [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
Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for...
3
by: Tony Johansson | last post by:
Hello Experts!! I have two small classes called Intvektor and Matris shown below and a main. Class Intvektor will create a one dimension array of integer by allocate memory dynamically as you...
17
by: Bushido Hacks | last post by:
I've come up with a good set of Matrix codes recently when I cam to a road block. I would like to know if I should destroy an object to create a new object with new dimmensions. class Matrix{...
3
by: Darleen | last post by:
I am seeking conceptual here on how to get started with a "3D Matrix" in Access. We run a training center which holds multiple classes in multiple cities at multiple times. So I need to create a...
2
by: Jan Callewaert | last post by:
When compiling a library with the intel compiler icpc, I get the following warning: ld: warning: creating a DT_TEXTREL in object This happens with icc-9.0.030 and ld-2.16.1. However, this...
4
by: tshad | last post by:
I am trying to set up an Image authorization where you type in the value that is in a picture to log on to our site. I found a program that is supposed to do it, but it doesn't seem to work. ...
4
by: crescent_au | last post by:
Hi all, I'm doing some research online on creating php-based multi-level marketing (MLM) system. It seems like a complicated system to build as I need to create one from scratch. I'd like to...
2
by: John | last post by:
Hi there, I need to create a rectangle between two points so that I can check what is inside it using Contains(). The problem I am having is how to make the rectangle be able to cope with the...
7
by: victory2006 | last post by:
i need help to make a board such as this: -----------Col 0--Col 1--Col 2 Row 0------1------1--------0 Row 1------0------0--------1 Row 2------1------1--------1 (Please ignore the dashes,...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.