How do I index from the second position to the last to the first?

Jiàn-gēng Chiōu

Suppose I have a list u = [1, 2, 3, 4, 5], and u[1:] returns [2, 3, 4, 5]. I wonder what indexing returns [2, 3, 4, 5, 1], going from the second position to the last and then the first?

Cory Kramer

You can make a general function that does this at any point in your list, just by adding two slices. This was an intentional design as to why slicing is half-open (includes left index, but excludes right index)

def rotate(l, i):
    return l[i:] + l[:i]

>>> u = [1, 2, 3, 4, 5]

>>> rotate(u, 1)
[2, 3, 4, 5, 1]
>>> rotate(u, 2)
[3, 4, 5, 1, 2]

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

How do I extract the second to last occurrence of a string in MySQL?

How do i get first date and Last date from month and year |Excel |Used Spinner for Month and Year|

how do i get last result in group by MySQL query (not the first)

Summing for each first level index in last row of second level index in a specific column

How do i pull last element from a set of nodes?

In gremlin (specifically tinkerpop), how do you query and then make a second query without ANY of the results from the first query?

How can I segue to the second tab of a tab bar controller from the first tab?

How can I row bind the unmatch data in the column of first table from the second table

How do I group the first four columns and the last four columns using Dplyr?

How do I print the first line of a file after sorting without the last value in powershell?

How do I get code to read both first and last and values of a randomly generated array?

I need the Index of the last item in a List, how?

Undefined index errors - How do I set the last amount after discount?

How can I get first 5 and last 1 records from table mysql?

How can I remove the left and right space from first & last images?

How can I split string in R from first square bracket and last round bracket?

How can I sum the second digit to the last digit in an integer on java?

Remove duplicate index value from first array, manipulate second as per first(at some specific conditions)

How do I setup a second component with a UIPickerView

How to keep track of last position of "i" when reading in different integers from multiple files into a 1dim-array?

In Python, how to compare two csv files based on values in one column and output records from first file that do not match second

How to match something from the last symbol before a given position?

How do i delete the last 3 commits?

How do I get the last character with jquery

How do I absolute position a div from left to the right using media queries?

How do I remove the last input from java HashMap using LinkedHashMap?

How do i get the last file from directory by the file name and then create the next file?

How to initialize second glove model with solution from first?

How to select range in Excel ListColumn's DataBodyRange from second to second last cell with VBA

TOP 一覧

  1. 1

    Unity:未知のスクリプトをGameObject(カスタムエディター)に動的にアタッチする方法

  2. 2

    セレンのモデルダイアログからテキストを抽出するにはどうすればよいですか?

  3. 3

    Ansibleで複数行のシェルスクリプトを実行する方法

  4. 4

    tkinterウィンドウを閉じてもPythonプログラムが終了しない

  5. 5

    Crashlytics:コンパイラー生成とはどういう意味ですか?

  6. 6

    GoDaddyでのCKEditorとKCfinderの画像プレビュー

  7. 7

    Windows 10 Pro 1709を1803、1809、または1903に更新しますか?

  8. 8

    Chromeウェブアプリのウェブビューの高さの問題

  9. 9

    モーダルダイアログを自動的に閉じる-サーバーコードが完了したら、Googleスプレッドシートのダイアログを閉じます

  10. 10

    Windows 10の起動時間:以前は20秒でしたが、現在は6〜8倍になっています

  11. 11

    Reactでclsxを使用する方法

  12. 12

    ファイル内の2つのマーカー間のテキストを、別のファイルのテキストのセクションに置き換えるにはどうすればよいですか?

  13. 13

    MLでのデータ前処理の背後にある直感

  14. 14

    グラフからテーブルに条件付き書式を適用するにはどうすればよいですか?

  15. 15

    Pythonを使用して同じ列の同じ値の間の時差を取得する方法

  16. 16

    mutate_allとifelseを組み合わせるにはどうすればよいですか

  17. 17

    ネットワークグラフで、ネットワークコンポーネントにカーソルを合わせたときに、それらを強調表示するにはどうすればよいですか?

  18. 18

    テキストフィールドの値に基づいて UIslider を移動します

  19. 19

    BLOBストレージからデータを読み取り、Azure関数アプリを使用してデータにアクセスする方法

  20. 20

    PowerShellの分割ファイルへのヘッダーの追加

  21. 21

    ソートされた検索、ターゲット値未満の数をカウント

ホットタグ

アーカイブ