重新排列列表中元素的顺序

时间:2018-11-12 14:13:06

标签: python python-3.x python-2.7

我正在尝试重新排列列表中元素的顺序。

我想将i + 1位置的元素移到列表的末尾,

len(list)-2次。

例如,如果我有

>>> 5 6 7 8 9 10 11 12 13 14 15

5 7 8 9 10 11 12 13 14 15 6

5 7 9 10 11 12 13 14 15 6 8

5 7 9 11 12 13 14 15 6 8 10

5 7 9 11 13 14 15 6 8 10 12

5 7 9 11 13 15 6 8 10 12 14

5 7 9 11 13 15 8 10 12 14 6

5 7 9 11 13 15 8 12 14 6 10

5 7 9 11 13 15 8 12 6 10 14

5 7 9 11 13 15 8 12 6 14 10 # desired output

下面是我的代码,但我只是想不出一种从列表中删除第i + 1个元素并将其附加在列表末尾的方法。

代码返回一个错误,指出弹出索引超出范围,并且令人沮丧。

def rearrange(sequence):
    test_list = list(sequence)

    length = len(test_list)
    for i in range(length - 2):
        stray_element = test_list.pop(i + 1)
        test_list.append(stray_element)

1 个答案:

答案 0 :(得分:2)

您发布的代码(在对其进行编辑以使其返回并提供输入序列之后)对我来说很好用,并可以产生所需的输出。

private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
    Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());

    AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
    mAuth.signInWithCredential(credential)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        // Sign in success, update UI with the signed-in user's information
                        Log.d(TAG, "signInWithCredential:success");
                        FirebaseUser user = mAuth.getCurrentUser();
                        int coins = 100;
                        String name, email;
                        name = user.getDisplayName();
                        email = user.getEmail();
                        //updateUI(user);
                        storeUserCredentials(name, email, coins);
                    } else {
                        // If sign in fails, display a message to the user.
                        Log.w(TAG, "signInWithCredential:failure", task.getException());
                        Toast.makeText(SignUpActivity.this, "Error : " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                }
            });
}

public void storeUserCredentials(String name, String email, int coins){
    progressBar.setVisibility(View.VISIBLE);
    User user = new User(name, email, coins);

    mDatabase.getReference("Users")
            .child(mAuth.getCurrentUser().getUid())
            .setValue(user)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    progressBar.setVisibility(View.GONE);
                    if (task.isSuccessful()){
                        Toast.makeText(SignUpActivity.this, "Registration Successful!", Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(SignUpActivity.this, MainActivity.class));
                        finish();
                    } else {
                        if (task.getException() instanceof FirebaseAuthUserCollisionException){
                            Toast.makeText(SignUpActivity.this, "The email is already used", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(SignUpActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                        }

                    }
                }
            });
}

输出:

my_sequence = [5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]

def rearrange(sequence):
    test_list = list(sequence)

    length = len(test_list)
    for i in range(length - 2):
        stray_element = test_list.pop(i + 1)
        test_list.append(stray_element)
    return test_list

print(rearrange(my_sequence))

(主要是由于格式...而发布为答案而不是评论)