[SciPy-user] problems with copying / comparing lists, containing arrays ?

Stef Mientki s.mientki@ru...
Wed Dec 12 18:16:23 CST 2007


hello,

I've a list, that contains an (numpy) array on one or more locations.
This list acts as a structure to hold all kinds of objects (an array is 
also such an object).
This list is "self.Input_Value" in the code below.
Initially this list is just [ None, None, None, None, None ]
Now some part of the program may fill elements in this list, so it 
become something like this (in practice the array will be much larger)
  [None, array([ 0.        ,  1.77857046,  2.86459363,  2.83518923,  
1.70180685,
  -0.09423228, -1.85357884, -2.89117103, -2.80298683, -1.62336376]), 
None, None, None, None]

I want to test if the array has changed, so I compare it to 
"self.Input_Value_Old",
      if self.Input_Value != self.Input_Value_Old:

If changes are found, I copy Input_Value to Input_Value_Old
      self.Input_Value_Old = copy.copy ( self.Input_Value )

But the next comparison gives an exception,
So the next piece of code

    # Check for any changes
    print 'Check Inputs',self.Input_Value
    print 'Check Inputs',self.Input_Value_Old
    try:
      if self.Input_Value != self.Input_Value_Old:
        print 'INPUTS CHANGED',self.Name
        Change = True
      else :
        print 'INPUT;',self.Name,self.Input_Value
    except:
      print 'ERROR'
    print 'DONE'

Generates the following output:

Check Inputs [None, array([ 0.        ,  1.77857046,  2.86459363,  
2.83518923,  1.70180685,
       -0.09423228, -1.85357884, -2.89117103, -2.80298683, 
-1.62336376]), None, None, None, None]
Check Inputs [None, array([  0.00000000e+00,   1.76335576e+00,   
2.85316955e+00,
         2.85316955e+00,   1.76335576e+00,   3.67381906e-16,
        -1.76335576e+00,  -2.85316955e+00,  -2.85316955e+00,
        -1.76335576e+00]), None, None, None, None]
ERROR
DONE

Each time some value in the array changes, the whole array is generated 
as a new array.
I discovered that if I change the array in place (which is of course the 
final solution),
the exception doesn't occur, but the change is also never detected,
because probably  the next statement is never True
      if self.Input_Value != self.Input_Value_Old:

Now with in place changing of the array, and a deepcopy,
the exception error occurs again and besides that a deepcopy has some 
trouble with other objects.

Is there another way to detect the changes in the array,
or is it better to set a flag when the array is changed ?

thanks,
Stef Mientki



More information about the SciPy-user mailing list