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

Hope someone helps.

1
  • 1
    not familiar with python, so am not sure what this does. Seems like it could be explained in a sentence. Then I could try to help. Commented May 16, 2009 at 13:27

2 Answers 2

6

I'm fairly new to Python but if I understand the code correctly, it reconstructs a list from a given offset into every item following offset+1 and the item at the offset.

Running it seems to confirm this:

>>> 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']

In Javascript can be written:

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

Same entire sequence would go:

>>> 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"]

This works because splice is destructive to the array but returns removed elements, which may then be handed to concat.

Sign up to request clarification or add additional context in comments.

3 Comments

Is it obvious that the last sentence is contrived just to fit in some links to the methods on MDC? :-)
It is an imaginary command line. I ran the code with FireBug but added the >>> in the end simply to make it look the same as the Python block. :-)
@Borgar, I will buy your imaginary command line. How much does it cost, in Quatloos?
1

You will want to look at Array.slice()

var temp=indices.slice(i+1).concat(indices.slice(i, i+1));
var arr=[];
for (var j=0; j<temp.length; j++){
   arr[j+i]=temp[i];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.