Python modifies the original variable after the function call [duplicate] - python

This question already has answers here:
Numpy array assignment with copy
(3 answers)
Closed 3 years ago.
I am wondering if someone can explain why Python modifies the original variable after assigning it to another variable and then passing the second variable the function call:
Consider the following example code:
Assume A is the original variable:
A=np.array(([1,20,30,40,10,5,60]))
B=A
B.sort()
print(A)
print(B)
The output for both is the same:
[ 1 5 10 20 30 40 60]
[ 1 5 10 20 30 40 60]
A is the original variable and I assigned it the B and then I sort B, then why both A and B are sorted ? what if I want to sort B only and compare it to A

if you say B=A where A is an Array, Python just makes a new Pointer to A
You can do
A = B[:]
to copy Array

Related

List assigning to another variable, then original variable also changes when there will happen an operation on child variable [duplicate]

This question already has answers here:
How do I clone a list so that it doesn't change unexpectedly after assignment?
(24 answers)
Closed last month.
a = [1,2,4]
b=a
b.insert(1,45)
print(a,b)
results
a = [1,45,2,4]
b = [1,45,2,4]
why a is changing
is there any way, where b will change
That's because you're just reassigning the same instance. You need to do this instead of b=a:
b = a.copy()

Python Comma Seperate Variables [duplicate]

This question already has answers here:
How does Python's comma operator work during assignment?
(3 answers)
Closed last month.
I am trying to understand how this comma separated solution works and how it is called, example:
b, c=0, 5
print(b,c)
>>0 5
Why is variable 'b' value 0?
Why is variable 'c' value 5?
Comma separated values are interpreted as tuple, which can be unpacked:
t = 0,5 # sames as t = (0,5)
print(type(t))
a,b = t
print(a,b)
Out:
<class 'tuple'>
0 5

why is a = [1,2,3] and a[:] not the same [duplicate]

This question already has answers here:
What is the difference between list and list[:] in python?
(7 answers)
Closed 3 years ago.
I have this below code
a = [1,2,3]
b = a
a is b #shows True
b = a[:]
a is b #shows False
a == b # shows True
I thought the value of [1,2,3] and a[:] would have the same id and they are the exact same object.
What exactly happens when a[:] is assigned to b?
I'm sorry if this question has already been asked before, could not find a perfect answer
a[:] is a shallow copy of a list (as a slice). It has the same numeric value, but it is not the original list. == Checks value, "is" is value and "identity" of the value.

Check if two variables are equal from three choices [duplicate]

This question already has answers here:
Python: determining whether any item in sequence is equal to any other
(5 answers)
Closed 3 years ago.
I have three integers, and I need to check if two of them are equal. The code I have is quite ugly:
a = 5
b = 7
c = 5
if a == b or b == c or a == b:
pass
I wonder if there is a better alternative to this kind of comparation.
You could just build a set and check the resulting length:
a = 5
b = 7
c = 5
if len({a,b,c}) < 3:
pass
Since you mention in your real case the variables are lists, you could convert them to tuples which are hashable and hence can build a set from them. So instead you could do:
a = [5, 2]
b = [7, 2]
c = [5, 2]
if len(set(map(tuple, [a,b,c]))) < 3:
pass
Because you are having lists, you won't be able to use a set directly. If so, you will get an error:
TypeError: unhashable type: 'list'
Convert your lists to tuples, then use the following:
if len(set([tuple(a),tuple(b), tuple(c)])) < 3:
pass

Forcing Python to pass a variable by value [duplicate]

This question already has answers here:
What do lambda function closures capture?
(7 answers)
Creating functions (or lambdas) in a loop (or comprehension)
(6 answers)
Closed 3 years ago.
Let's say I want to create a list of functions programmatically, e.g. I want to create nine functions that, given a number in input, they add it respectively 1, 2, ... 9 and return it.
My take would be to write the following code:
>>> functions = []
>>> for i in range(1,10):
... functions.append(lambda x : x + i)
...
>>> for f in functions:
... print(f(0), end=' ')
However, the ouput is
9 9 9 9 9 9 9 9 9
Instead of the expected
1 2 3 4 5 6 7 8 9
I understand that this has to do with how Python deals with function arguments, i.e. when I create my lambda, I bound it to the reference of i, and not to its value.
How can I get around this? Is there a way to force Python to make a copy of i, instead of passing is reference?

Categories

Resources