Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to add an object to an array list but since I'm adding the actual object when I try to reset the array thereafter, all the values in the array are reset. Is there an actual way how I can add a monitor object to the array and change the values and not affect the ones I've already saved in the array?

Thanks

Code:

arrayList = []

for x in allValues:


        result = model(x)

        arrayList.append(wM)

        wM.reset()

where wM is a monitor class - whihc is being calculated / worked out in the model method

share|improve this question
3  
I don't understand your problem. Some simple code showing what's going wrong could help. – Chris Lutz Feb 4 '10 at 2:23
3  
What's an "array list"? A list? – S.Lott Feb 4 '10 at 2:50
yes its a list. – Lilz Feb 4 '10 at 3:05

4 Answers

up vote 6 down vote accepted

Is your problem similar to this:

l = [[0]] * 4
l[0][0] += 1
print l # prints "[[1], [1], [1], [1]]"

If so, you simply need to copy the objects when you store them:

import copy
l = [copy.copy(x) for x in [[0]] * 4]
l[0][0] += 1
print l # prints "[[1], [0], [0], [0]]"

The objects in question should implement a __copy__ method to copy objects. See the documentation for copy. You may also be interested in copy.deepcopy, which is there as well.

EDIT: Here's the problem:

arrayList = []
for x in allValues:
    result = model(x)
    arrayList.append(wM) # appends the wM object to the list
    wM.reset()           # clears  the wM object

You need to append a copy:

import copy
arrayList = []
for x in allValues:
    result = model(x)
    arrayList.append(copy.copy(wM)) # appends a copy to the list
    wM.reset()                      # clears the wM object

But I'm still confused as to where wM is coming from. Won't you just be copying the same wM object over and over, except clearing it after the first time so all the rest will be empty? Or does model() modify the wM (which sounds like a terrible design flaw to me)? And why are you throwing away result?

share|improve this answer
+1 I am guessing OP's problem relates to this – ghostdog74 Feb 4 '10 at 2:30
THANK YOU SO SO SO MUCH :) – Lilz Feb 4 '10 at 3:09
@Lily - If you had read and thought about either my original answer or ghostdog74's answer, you could have done this on your own. You should consider reading the documentation, particularly about immutable vs. mutable objects and the copy module. – Chris Lutz Feb 4 '10 at 3:22
Great answer, Mr. Lutz! – jathanism Feb 4 '10 at 3:34

while you should show how your code looks like that gives the problem, i think this scenario is very common. See copy/deepcopy

share|improve this answer
+1 for beating me. – Chris Lutz Feb 4 '10 at 2:29

You need to create a copy of the list before you modify its contents. A quick shortcut to duplicate a list is this:

mylist[:]

Example:

>>> first = [1,2,3]
>>> second = first[:]
>>> second.append(4)
>>> first
[1, 2, 3]
>>> second
[1, 2, 3, 4]

And to show the default behavior that would modify the orignal list (since a name in Python is just a reference to the underlying object):

>>> first = [1,2,3]
>>> second = first
>>> second.append(4)
>>> first
[1, 2, 3, 4]
>>> second
[1, 2, 3, 4]

Note that this only works for lists. If you need to duplicate the contents of a dictionary, you must use copy.deepcopy() as suggested by others.

share|improve this answer

If i am correct in believing that you are adding a variable to the array but when you change that variable outside of the array, it also changes inside the array but you don't want it to then it is a really simple solution.

When you are saving the variable to the array you should turn it into a string by simply putting str(variablename). For example:

array.append(str(variablename))

Using this method your code should look like this:

arrayList = []
for x in allValues:
    result = model(x)
    arrayList.append(str(wM))        #this is the only line that is changed.
    wM.reset()
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.