I want to represent an NxN matrix by a list containing N lists, each
of which has N elements. Initially the elements are set to " ". For
N=2, I write x = [" "][:]*2 #assignment creates references, not copies! x
[' ', ' ']y = [x[:]]*2 y
[[' ', ' '], [' ', ' ']]
But if I assign y[0][0], I y[1,0] changes as welly[0][0]=1 y
[[1, ' '], [1, ' ']]
I am clearly creating references in spite of trying not to. The
problem is completely solved by using copy.copy()x = [" "][:]*2 y=[] for i in range(2):
y.append(copy.copy(x)) y
[[' ', ' '], [' ', ' ']] y[0][0]=1 y
[[1, ' '], [' ', ' ']]
I fail to see the error in my first attempt. Where is the fly in the
ointment?
Thomas Philips 4 1237
Thomas Philips a écrit : I want to represent an NxN matrix by a list containing N lists, each of which has N elements. Initially the elements are set to " ". For N=2, I write
x = [" "][:]*2 #assignment creates references, not copies! x [' ', ' '] y = [x[:]]*2 [...] I fail to see the error in my first attempt. Where is the fly in the ointment?
Thomas Philips
Your trboules come from this line: y = [x[:]]*2 because you do not copy
the x twice but just assign a copy of x twice...
see the following: x = [" "]*2 x
[' ', ' '] x = [" "]*2 y = [x]*2 y
[[' ', ' '], [' ', ' ']] id(y[0])
8303600 id(y[1])
8303600 y = [[" "] *2, [" "]*2] id(y[0])
8306544 id(y[1])
8305424
--
Yermat
You might try:
x=n*[n*[' ']]
But I always question creating arrays this
way in Python. It is normally much better
to start with an empty list and build it as
you go using .append() method.
HTH,
Larry Bates
Syscon, Inc.
"Thomas Philips" <tk****@hotmail.com> wrote in message
news:b4**************************@posting.google.c om... I want to represent an NxN matrix by a list containing N lists, each of which has N elements. Initially the elements are set to " ". For N=2, I write
x = [" "][:]*2 #assignment creates references, not copies! x [' ', ' ']y = [x[:]]*2 y [[' ', ' '], [' ', ' ']] But if I assign y[0][0], I y[1,0] changes as welly[0][0]=1 y [[1, ' '], [1, ' ']]
I am clearly creating references in spite of trying not to. The problem is completely solved by using copy.copy()x = [" "][:]*2 y=[] for i in range(2): y.append(copy.copy(x)) y [[' ', ' '], [' ', ' ']] y[0][0]=1 y
[[1, ' '], [' ', ' ']]
I fail to see the error in my first attempt. Where is the fly in the ointment?
Thomas Philips
"Larry Bates" <lb****@swamisoft.com> wrote in
news:dd********************@comcast.com: You might try:
x=n*[n*[' ']]
But I always question creating arrays this way in Python. It is normally much better to start with an empty list and build it as you go using .append() method.
Its just as well you question this as it creates a list containing n copies
of the same list.
A better way to write your example:
x = [ n*[' '] for i in range(n) ]
1. Aaaargh.........!
2. I see, said the blind man, (as he picked up his hammer and
nail).......................
3. Thanks for the insight!
Thomas Philips This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Henning Kage |
last post by:
I'm using Python only for some months now and I'm wondering, whether such
assignments as above are creating bitwise copies of an object or just
recieve a reference. That means I wanted to know,...
|
by: Andrew |
last post by:
Can someone help explain the following?
Why is it that elements 5 - 9 in Bar::group do not seem to resolve to
elements 0 - 4 of Bar::group when de-referenced?
If I am following this correctly...
|
by: Scott Danzig |
last post by:
I'm curious.. let's say you had:
int val = 5;
int a()
{
return val;
}
int &b()
{
|
by: Michelle Collier-Moore |
last post by:
Please could someone offer some advice regarding adding references to
an Access database?
I tried to open a project a few days ago sent to me by someone whose
developer had left the company. I...
|
by: films |
last post by:
I understand the concept.
Serialization of a class will add all the sub-objects of the class to
the stream if there are also serializible.
So say I have:
class Author
{
| |
by: naveid |
last post by:
I have an array (List<T>) containing tens of thousands of items. I need
to maintain copies of the array, sorted in a few different ways. I'm
working on Windows Mobile so memory is a constraint. To...
|
by: howa |
last post by:
from PHP manual, it said:
Do not use return-by-reference to increase performance, the engine is
smart enough to optimize this on its own
------------------
Why?
|
by: mk |
last post by:
Hello everyone,
I'm storing functions in a dictionary (this is basically for cooking up
my own fancy schmancy callback scheme, mainly for learning purpose):
.... return "f2 " + arg
.......
|
by: Calvin Spealman |
last post by:
On Thu, Jul 17, 2008 at 7:45 AM, mk <mrkafk@gmail.comwrote:
As was pointed out already, this is a basic misunderstanding of
assignment, which is common with people learning Python.
To your...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |