Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

List weirdness - what the heck is going on here?

0 views
Skip to first unread message

Rotwang

unread,
Jan 27, 2010, 9:06:28 PM1/27/10
to
Hi all, I've been trying to make a class with which to manipulate sound
data, and have run into some behaviour I don't understand which I hope
somebody here can explain. The class has an attribute called data, which
is a list with two elements, one for each audio channel, each of which
is a list containing the audio data for that channel. It also has
various methods to write data such as sine waves and so on, and a method
to insert data from one sound at the start of data from another.
Schematically, the relevant bits look like this:

class sound:
def f(self):
self.data = [[0]]*2

def insert(self, other):
for c in xrange(2):
self.data[c][0:0] = other.data[c]

However, the insert method doesn't work properly; x.insert(y) adds two
copies of y's data to the start of x's data, instead of one. From a
session in IDLE:

>>> x = sound()
>>> y = sound()
>>> x.f()
>>> y.f()
>>> x.data
[[0], [0]]
>>> x.insert(y)
>>> x.data
[[0, 0, 0], [0, 0, 0]]

But suppose I replace the line

self.data = [[0]]*2

with

self.data = [[0] for c in xrange(2)]

Then it works fine:

>>> x = sound()
>>> y = sound()
>>> x.f()
>>> y.f()
>>> x.data
[[0], [0]]
>>> x.insert(y)
>>> x.data
[[0, 0], [0, 0]]

Can anybody tell me what's going on?

Owen Jacobson

unread,
Jan 27, 2010, 9:18:34 PM1/27/10
to
On 2010-01-27 21:06:28 -0500, Rotwang <sg...@hotmail.co.uk> said:

> Hi all, I've been trying to make a class with which to manipulate sound
> data, and have run into some behaviour I don't understand which I hope
> somebody here can explain. The class has an attribute called data,
> which is a list with two elements, one for each audio channel, each of
> which is a list containing the audio data for that channel. It also has
> various methods to write data such as sine waves and so on, and a
> method to insert data from one sound at the start of data from another.
> Schematically, the relevant bits look like this:
>
> class sound:
> def f(self):
> self.data = [[0]]*2

Consider that this is equivalent to

def f(self):
x = [0]
self.data = [x, x]

self.data is now a list containing two references to the list
referenced by x -- so changes via either of the elements of self.data
will affect both elements. Your comprehension version creates a list
containing two distinct list objects, so this doesn't happen.

> Can anybody tell me what's going on?

-o

alex23

unread,
Jan 27, 2010, 9:59:26 PM1/27/10
to
Rotwang <sg...@hotmail.co.uk> wrote:
> Can anybody tell me what's going on?

Your problem is basically this:

>>> a = [1]
>>> b = [a] * 2
>>> b
[[1], [1]]
>>> a.append(2)
>>> b
[[1, 2], [1, 2]]

The expression '[a] * 2' doesn't make two copies of list of a list of
a, it makes two nested _references_ to it. When you modify the 'first'
list, you're seeing that changed reflected in the second reference.

The list comprehension, however, is returning a _new_ list each time.
Changing its contents doesn't affect any of the other independently
created lists.

Arnaud Delobelle

unread,
Jan 28, 2010, 2:21:28 AM1/28/10
to

Rotwang

unread,
Jan 28, 2010, 7:11:05 AM1/28/10
to

Thanks, and likewise to everyone else who replied.

0 new messages