Dec-13-2018, 09:34 AM
Hi, I'm learning Python through an example below. However, I see a line that is weird. The line is marked as
# mark-1
. I understood that "," is used to create a tuple. And a tuple is a sequence of immutable Python objects. However, the line of code contains a part that puzzles me: nums[i] = nums[i]
- this part seems to change the element of the tuple. What's going on? class Example: def perm(self, nums): def backtrack(start, end): if start == end: ans.append(nums[:]) for i in range(start, end): nums[start], nums[i] = nums[i], nums[start] # mark-1 backtrack(start+1, end) nums[start], nums[i] = nums[i], nums[start] # mark-2 ans = [] backtrack(0, len(nums)) return ans # Print the ans[] def print(self, ans): for x in ans: print(" ".join(map(str, x)))