Problem with assignment. Python error or mine?

Tim Williams tjandacw at cox.net
Fri Dec 22 08:41:00 EST 2017


On Thursday, December 21, 2017 at 12:18:11 PM UTC-5, MarkA wrote:
> On Thu, 21 Dec 2017 07:05:33 -0800, rafaeltfreire wrote:
> From docs.python.org:
> 
> 8.10. copy — Shallow and deep copy operations
> 
> Source code: Lib/copy.py
> 
> Assignment statements in Python do not copy objects, they create bindings 
> between a target and an object. For collections that are mutable or 
> contain mutable items, a copy is sometimes needed so one can change one 
> copy without changing the other. This module provides generic shallow and 
> deep copy operations (explained below)...
> 
> 
> > Dear community, I am having the following problem when I am assigning
> > the elements of a vector below a certain number to zero or any other
> > value.
> > I am creating a new variable but Python edits the root variable. Why?
> > 
> > import numpy as np
> > 
> > X=np.arange(1, 10000, 1) #root variable x1=X x1[x1<10000]=0
> > 
> > print(X)
> > Out[1]: array([ 0.,  0.,  0., ...,  0.,  0.,  0.])
> > 
> > Why????????? It is supposed to be the original value Thank you for your
> > time Rafael
> 
> 
> 
> -- 
> MarkA
> 
> We hang petty theives, and appoint the great theives to public office
>   -- Aesop

Shouldn't the OP just create a list for what he want's to do?

X = list(np.arange(1, 10000, 1)) #root variable x1=X x1[x1<10000]=0

Then I think his other statements would do what he expects, no?



More information about the Python-list mailing list