Is there a standardized method to swap two variables in Python?

后端 未结 7 970
天涯浪人
天涯浪人 2020-11-22 00:09

In Python, I\'ve seen two variable values swapped using this syntax:

left, right = right, left

Is this considered the standard way to swap

7条回答
  •  北恋
    北恋 (楼主)
    2020-11-22 00:30

    I would not say it is a standard way to swap because it will cause some unexpected errors.

    nums[i], nums[nums[i] - 1] = nums[nums[i] - 1], nums[i]
    

    nums[i] will be modified first and then affect the second variable nums[nums[i] - 1].

提交回复
热议问题