Re: Problem with assignment. Python error or mine?
On Friday, December 22, 2017 at 8:41:29 AM UTC-5, Tim Williams wrote: > 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, 1, 1) #root variable x1=X x1[x1<1]=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, 1, 1)) #root variable x1=X x1[x1<1]=0 > > Then I think his other statements would do what he expects, no? Disregard what I just posted. I didn't think this through enough. -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with assignment. Python error or mine?
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, 1, 1) #root variable x1=X x1[x1<1]=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, 1, 1)) #root variable x1=X x1[x1<1]=0 Then I think his other statements would do what he expects, no? -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with assignment. Python error or mine?
On 21/12/17 19:06, John Ladasky wrote: > On Thursday, December 21, 2017 at 7:37:39 AM UTC-8, MRAB wrote: > >> Python never makes a copy unless you ask it to. >> >> What x1=X does is make the name x1 refer to the same object that X >> refers to. No copying. > > Well, except with very simple, mutable data types like scalars... compare > this: > x=5 y=x x,y > (5, 5) x+=1 x,y > (6, 5) > > To this: > a=[1,2,3] b=a a,b > ([1, 2, 3], [1, 2, 3]) a[1]=9 a,b > ([1, 9, 3], [1, 9, 3]) > Except ints aren't mutable and there's still no copying. For x += 1 (where x is e.g. an int) read x = x + 1 Duncan -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with assignment. Python error or mine?
2017-12-21 22:06 GMT+03:00 John Ladasky: > On Thursday, December 21, 2017 at 7:37:39 AM UTC-8, MRAB wrote: > > > Python never makes a copy unless you ask it to. > > > > What x1=X does is make the name x1 refer to the same object that X > > refers to. No copying. > > Well, except with very simple, mutable data types like scalars... compare > this: > No copy means no copy, it is the rule! What you see is really new binding operation under the hood. 'x=1; x += 1', means calculate x+1 and bind it to the same name. Compare it to this example: >>> tpl = ((1,2),(3,4)) >>> tpl += ((1,2),) >>> tpl ((1, 2), (3, 4), (1, 2)) No copy, new binding to the same name :) With kind regards, -gdg -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with assignment. Python error or mine?
On Thursday, December 21, 2017 at 7:37:39 AM UTC-8, MRAB wrote: > Python never makes a copy unless you ask it to. > > What x1=X does is make the name x1 refer to the same object that X > refers to. No copying. Well, except with very simple, mutable data types like scalars... compare this: >>> x=5 >>> y=x >>> x,y (5, 5) >>> x+=1 >>> x,y (6, 5) To this: >>> a=[1,2,3] >>> b=a >>> a,b ([1, 2, 3], [1, 2, 3]) >>> a[1]=9 >>> a,b ([1, 9, 3], [1, 9, 3]) -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with assignment. Python error or mine?
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, 1, 1) #root variable x1=X x1[x1<1]=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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with assignment. Python error or mine?
On 2017-12-21 15:05, rafaeltfre...@gmail.com wrote: 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, 1, 1) #root variable x1=X x1[x1<1]=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 Python never makes a copy unless you ask it to. What x1=X does is make the name x1 refer to the same object that X refers to. No copying. As you're using numpy, you can use the .copy method: x1 = X.copy() This makes the name x1 refer to a new copy of the object that X refers to. -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with assignment. Python error or mine?
Em quinta-feira, 21 de dezembro de 2017 16:21:57 UTC+1, Neil Cerutti escreveu: > On 2017-12-21, rafaeltfre...@gmail.comwrote: > > 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, 1, 1) #root variable > > np.arange creates an object. The assignment makes X refer to that > object. > > > x1=X > > X refers to the previous object, and then the assignment makes x1 > refer to that same object. > > -- > Neil Cerutti Ok, great thank you. I am kind of new in python. I use to program in MATLAB but I am trying to migrate. So, to fix it what should I do? because my X is an NMR spectrum of many samples. Thank you very much! Rafael -- https://mail.python.org/mailman/listinfo/python-list
Re: Problem with assignment. Python error or mine?
On 2017-12-21, rafaeltfre...@gmail.comwrote: > 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, 1, 1) #root variable np.arange creates an object. The assignment makes X refer to that object. > x1=X X refers to the previous object, and then the assignment makes x1 refer to that same object. -- Neil Cerutti -- https://mail.python.org/mailman/listinfo/python-list
Problem with assignment. Python error or mine?
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, 1, 1) #root variable x1=X x1[x1<1]=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 -- https://mail.python.org/mailman/listinfo/python-list