问题
I came across this problem when attempting to learn python. Consider the following function:
def swap0(s1, s2):
assert type(s1) == list and type(s2) == list
tmp = s1[:]
s1 = s2[:]
s2 = tmp
return
s1 = [1]
s2 = [2]
swap0(s1, s2)
print s1, s2
What will s1 and s2 print?
After running the problem, I found that the print statement will print 1 2. It seems that the value of s1 and s2 did not change from the swap0
function. The only explanation that I could think of was because of the line.
tmp = s1[:]
Since s1[:] is a copy, this makes sense that the value of s1 will not change in the function call. However because the parameter of swap0
is (s1, s2), I am not sure if after doing tmp = s1[:]. Anytime I do
s1 = something...
it will be a reference to the copy of s1, instead of s1 itself. Can someone offer a better explanation? Thanks.
回答1:
It's because it assigns new values to s1
and s2
inside the swap0
function. These assignments do not propagate outside the function. You'll see that it works if you just copy and paste the function body in the place of the function call.
You can work around this by modifying the objects referenced by the arguments, rather than the arguments themselves:
def swap0(s1, s2):
assert type(s1) == list and type(s2) == list
tmp = s1[:]
s1[:] = s2
s2[:] = tmp
However, the easier and better way to do a swap in Python is simply:
s1, s2 = s2, s1
This, too, will only swap those particular references to the lists, but not the list contents themselves.
回答2:
As it is, your final print
will print out the original values of s1
and s2
. This is because you're only swapping them within the scope of the function. Doing so will not affect their values outside the function (i.e. after their values after the function has been called)
If they are mutable types (list
, set
, dict
, etc), then you could modify them in-place inside swap
. However, that restricts swap
to work only on mutable types.
You are therefore better off returning the inputs in reversed order:
def swap(s1, s2):
return s2, s1
s1 = 'a'
s2 = 'b'
s1, s2 = swap(s1, s2)
print s1, s2 # prints 'b a'
Of course, you could do this all in one line as follows:
s1, s2 = s2, s1
Cheers!
回答3:
The other answers explain what's going wrong. Here's a version that does what you want:
def swap(s1, s2):
assert isinstance(s1, list) and isinstance(s2, list)
s1[:], s2[:] = s2[:], s1[:]
See also: isinstance vs. type
回答4:
Inside the function, you're rebinding local variables s1
and s2
with the values on the right hand side (which are also local since you're using slices to make copies). Even if you change the contents of those local variables, you won't change the contents of the lists in the calling scope because they no longer refer to the same lists.
回答5:
Here is a one-line function that accomplishes your goal:
swap = lambda x: (x[1], x[0])
回答6:
You can also do this by old swaping method using indexing and loop if both list have same length. This is kind of old school but will help in understanding indexing
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
b = [0, 9, 8, 7, 6, 5, 4, 3, 2, 1]
for i in range(0, len(a)):
a[i] = a[i] + b[i]
b[i] = a[i] - b[i]
a[i] = a[i] - b[i]
print(a)
print(b)
This will give the output as :
[0,9,8,7,6,5,4,3,2,1]
[1,2,3,4,5,6,7,8,9,0]
Or It can also be done using Xor. Xor operator is a bitwise operator which do the Xor operation between the operands for example.
a = 5 #0b101
b = 4 #0b100
c = a ^ b #0b001
Here 0b101
is a binary representation of 5 and 0b100
is a binary representation of 4 and when you Xor these you will the ouput as 0b001
i.e 1 . Xor return 1 output results if one, and only one, of the inputs to is 1. If both inputs are 0 or both are 1, 0 output results.
We can swap a two variables using Xor for eg:
a = 5 # 0b0101
b = 9 # 0b1001
a = a ^ b # Xor (0b0101, 0b1001) = 0b1100 (12)
b = a ^ b # Xor (0b1100, 0b1001) = 0b0101 (5)
a = a ^ b # Xor (0b1100, 0b0101) = 0b1001 (9)
print("a = {} and b = {}".format(a, b))
The Output will be a = 9 and b = 5
Similarly we can also swap two list by doing Xor operation on there items for eg:
a = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 ]
b = [ 0, 9, 8, 7, 6, 5, 4, 3, 2, 1 ]
for i in range(0, len(a)) :
a[i] = a[i] ^ b[i]
b[i] = a[i] ^ b[i]
a[i] = a[i] ^ b[i]
print(a)
print(b)
Output:
[0, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Lets Take another scenario, What if we need to swap the items within the list for eg:
we have a list like this x = [ 13, 3, 7, 5, 11, 1 ]
and we need to swap its item like this x = [ 1, 3, 5, 7 , 11, 13 ]
So we can do this by Using two bitwise operators Xor ^
and Compliments ~
Code :
# List of items
a = [ 13, 3, 7, 5, 11, 1 ]
# Calculated the length of list using len() and
# then calulated the middle index of that list a
half = len(a) // 2
# Loop from 0 to middle index
for i in range(0, half) :
# This is to prevent index 1 and index 4 values to get swap
# because they are in their right place.
if (i+1) % 2 is not 0 :
#Here ~i means the compliment of i and ^ is Xor,
# if i = 0 then ~i will be -1
# As we know -ve values index the list from right to left
# so a [-1] = 1
a[i] = a[i] ^ a[~i]
a[~i] = a[i] ^ a[~i]
a[i] = a[i] ^ a[~i]
print(a)
So Output will be [1, 3, 5, 7, 11, 13]
回答7:
yo can have this:
def swap(x , y):
x , y = y , x
return x , y
x = 5
y = 10
print ('x is {0} and y is {1}'.format(x,y)) # x is 5 and y is 10
x , y = swap(x,y) # doing swap
print ('x is {0} and y is {1}'.format(x,y)) # x is 10 and y is 5
回答8:
There is no need for function at all. a,b=b,a does the trick.
>>> a,b=1,2
>>> print (a,b)
(1, 2)
>>> a,b=b,a
>>> print (a,b)
(2, 1)
>>>
It works for arrays as well. But if you are so want the function here it is
def swap(a,b)
return b,a
来源:https://stackoverflow.com/questions/13167300/python-simple-swap-function