0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Python】特定の要素をリストの末尾に移動する関数

Posted at

作るもの

与えられたリスト(array)内の特定の要素(toMove)をリストの末尾に移動する関数。

実装

main.py
def moveElementToEnd(array, toMove):
    # 一時的なインデックスを初期化
    tmp = 0

    # リスト内の各要素に対してループ処理を開始
    for i in range(len(array)):
        # 現在の要素が移動させたい要素でない場合
        if array[i] != toMove:
            # 現在の要素と一時的な位置にある要素を入れ替え
            array[i], array[tmp] = array[tmp], array[i]
            # 一時的な位置を次の要素に更新
            tmp += 1

    # 要素の移動が終了したリストを返す
    return array


# テスト
if __name__ == "__main__":
    array = [2, 1, 2, 2, 2, 3, 4, 2]
    toMove = 2
    print(moveElementToEnd(array, toMove))

参考

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?