Python 配列の末尾からn個の要素を取得するサンプル

環境
Windows 11 Pro 64bit
Python 3.11

構文
配列名[-取得する要素数:]
array[-N:]のように、配列を取得する要素数のマイナスの値から末尾までスライスします。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
numbers = [21, 32, 43, 54, 65, 76]
tt = numbers[-3:]
print(tt)
numbers = [21, 32, 43, 54, 65, 76] tt = numbers[-3:] print(tt)
numbers = [21, 32, 43, 54, 65, 76]

tt = numbers[-3:]

print(tt)

実行結果
[54, 65, 76]

Python

Posted by arkgame