Pythonic Swap?

前端 未结 5 572
一整个雨季
一整个雨季 2021-01-08 00:25

I found that i have to perform a swap in python and i write something like this.

arr[first], arr[second] = arr[second], arr[first]

I suppos

5条回答
  •  感动是毒
    2021-01-08 00:35

    I suppose you could take advantage of the step argument of slice notation to do something like this:

    myarr[:2] = myarr[:2][::-1]

    I'm not sure this is clearer or more pythonic though...

提交回复
热议问题