0

b is

array([-0.06106568, -0.10843541, -0.0694688 ,  0.02464023, -0.03686665,
       -0.0582096 , -0.13476669, -0.08505708,  0.00391955, -0.12300518,
       -0.01183732, -0.05374973, -0.12300518, -0.05312849,  0.01963862,
        0.00155719, -0.10843541, -0.08490177, -0.08505708, -0.02026149,
       -0.01777489,  0.01183732, -0.11575136,  0.04278603, -0.0694688 ,
       -0.06106568, -0.08755022, -0.01660802, -0.06087603, -0.06582411])

a = b
a[a <= 0] = 0
a[a > 0] = 1

With the code above I would like to replace elements in a, but b also changes... Could you please explain me where is the mistake?

2
  • 1
    Well you assigned a named ref a to b with a=b, if you wanted a copy then a = np.copy(b) would give you a copy Commented Nov 10, 2016 at 11:00
  • I would like to make a copy...sorry I am new in python Commented Nov 10, 2016 at 11:00

3 Answers 3

3

By using expression a = b you actually copy the reference to b. If you want to copy its value, you should rather walk through all b's items and copy their values to a.

In numpy you should use copy function.

>>> import numpy
>>> b = numpy.array([-0.06106568, -0.10843541, -0.0694688 ,  0.02464023, -0.03686665,
...        -0.0582096 , -0.13476669, -0.08505708,  0.00391955, -0.12300518,
...        -0.01183732, -0.05374973, -0.12300518, -0.05312849,  0.01963862,
...         0.00155719, -0.10843541, -0.08490177, -0.08505708, -0.02026149,
...        -0.01777489,  0.01183732, -0.11575136,  0.04278603, -0.0694688 ,
...        -0.06106568, -0.08755022, -0.01660802, -0.06087603, -0.06582411])
>>> a = numpy.copy(b)
>>> a
array([-0.06106568, -0.10843541, -0.0694688 ,  0.02464023, -0.03686665,
       -0.0582096 , -0.13476669, -0.08505708,  0.00391955, -0.12300518,
       -0.01183732, -0.05374973, -0.12300518, -0.05312849,  0.01963862,
        0.00155719, -0.10843541, -0.08490177, -0.08505708, -0.02026149,
       -0.01777489,  0.01183732, -0.11575136,  0.04278603, -0.0694688 ,
       -0.06106568, -0.08755022, -0.01660802, -0.06087603, -0.06582411])
>>> a[a<= 0] = 0
>>> a[a> 0] = 1
>>> a
array([ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,
        0.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  1.,  0.,  0.,
        0.,  0.,  0.,  0.])
>>> b
array([-0.06106568, -0.10843541, -0.0694688 ,  0.02464023, -0.03686665,
       -0.0582096 , -0.13476669, -0.08505708,  0.00391955, -0.12300518,
       -0.01183732, -0.05374973, -0.12300518, -0.05312849,  0.01963862,
        0.00155719, -0.10843541, -0.08490177, -0.08505708, -0.02026149,
       -0.01777489,  0.01183732, -0.11575136,  0.04278603, -0.0694688 ,
       -0.06106568, -0.08755022, -0.01660802, -0.06087603, -0.06582411])

There are some native ways to do it, but if you work with mathematics, using only numpy is strongly recommended.

UPDATE

I don't have in my mind any non-numpy way which will cause entire compatibility with your case.

Sign up to request clarification or add additional context in comments.

3 Comments

What do you mean by "native ways" to do it ?
a=list(b) makes a a python list so the rest of the code wouldn't work as a[a<= 0] only makes sense with numpy arrays ...
I'm still a bit puzzled as to why you would not want to use numpy's copy when you are already working with numpy arrays ...
1

When you did a=b then you made a named ref to b so a and b were views on the same object, if you wanted a copy then use np.copy:

In [35]:
a = np.copy(b)
a[a<= 0] = 0
a[a> 0] = 1
a

Out[35]:
array([ 0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,  1.,  0.,  0.,  0.,  0.,
        0.,  1.,  1.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  1.,  0.,  0.,
        0.,  0.,  0.,  0.])

In [36]:
b

Out[36]:
array([-0.06106568, -0.10843541, -0.0694688 ,  0.02464023, -0.03686665,
       -0.0582096 , -0.13476669, -0.08505708,  0.00391955, -0.12300518,
       -0.01183732, -0.05374973, -0.12300518, -0.05312849,  0.01963862,
        0.00155719, -0.10843541, -0.08490177, -0.08505708, -0.02026149,
       -0.01777489,  0.01183732, -0.11575136,  0.04278603, -0.0694688 ,
       -0.06106568, -0.08755022, -0.01660802, -0.06087603, -0.06582411])

Comments

0

You are actually just pointing a to b, that both contain the same list. Essentially python variables are names that are given to objects. You want to create a new object containing the new list here. You have many options:

You could use the built-in list() function:

a = list(b)

Or you could import copy and use that.

import copy
a = copy.copy(b)

or in the case of numpy:

import numpy
a = np.copy(b)

1 Comment

Note that a and b are numpy arrays not lists.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.