我是一名python初学者。最近我看到这段代码:python:使用元组重新排列列表的内容
>>> words = ['I', 'turned', 'off', 'the', 'spectroroute']
>>> words[2], words[3], words[4] = words[3], words[4], words[2]
>>> words
['I', 'turned', 'the', 'spectroroute', 'off']
我对第二行感到困惑。看起来使用了元组,但我不明白为什么列表的顺序改变为结果?
它看起来像2号线是这样做的:
>>> tmp = words[2]
>>> words[2] = words[3]
>>> words[3] = words[4]
>>> words[4] = tmp
我的问题是:为什么在第二行的代码更改列表的结果呢?
在此先感谢
在您选择的Python参考中查找“元组赋值”。 – 2012-03-19 21:52:43
使用元组* *。 ','字符定义了一个元组,元素只是为了清晰和需要时的优先级。 – Daenyth 2012-03-19 22:36:26