我对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"]
这是因为splice对数组具有破坏性,但返回已删除的元素,然后可以将其传递给concat.
我对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"]
这是因为splice对数组具有破坏性,但返回已删除的元素,然后可以将其传递给concat.