indices[i:] = indices[i+1:] + indices[i:i+1]

希望有人帮助。

有帮助吗?

解决方案

我是相当新的Python的,但如果我理解正确的代码,它下面重建偏移+ 1从给定偏移到每一个项目的列表,并在项目的偏移量。

运行它似乎证实这样:

>>> indices = ['one','two','three','four','five','six']
>>> i = 2
>>> indices[i:] = indices[i+1:] + indices[i:i+1]
>>> indices
['one', 'two', 'four', 'five', 'six', 'three']

在的Javascript可以写成:

indices = indices.concat( indices.splice( i, 1 ) );

同整个序列会去:

>>> var indices = ['one','two','three','four','five','six'];
>>> var i = 2;
>>> indices = indices.concat( indices.splice( i, 1 ) );
>>> indices
["one", "two", "four", "five", "six", "three"]

此工作,因为剪接是破坏性到阵列但返回删除的元素,然后可将其交给的concat

scroll top