ホーム » ASUSTOR NAS » Python 負のインデックス

2020年7月
 1234
567891011
12131415161718
19202122232425
262728293031  

カテゴリー

アーカイブ

ブログ統計情報

  • 77,356 アクセス



Python 負のインデックス

次の所を読ませてもらっていて感じたことを…
けいしゅけのブログ薬局 情報館
https://keisyuke-blogyakkyoku.xyz/python-list-index
今私が習得するのにタイミングや更新サイクルが丁度良いので助かっている.


ASUSTOR NAS AS5202T に SSH 接続して Python を操作.

C:\Program Files\Microsoft Office\Office14>cd C:\Users\Iwao\AppData\Local\Temp

C:\Users\Iwao\AppData\Local\Temp>ssh -l Iwao -p 22 192.168.1.75
Password:
Iwao@AS5202T:/volume1/home/Iwao $ python
Python 2.7.10 (default, Aug 19 2015, 09:18:54)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> a=[0,1,2,3,4]
>>> print(a[0])
0
>>> print(a[4])
4
>>> print(a[5])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>> print(a[-1])
4
>>> print(a[-4])
1
>>> print(a[-5])
0
>>> print(a[-6])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
>>>  

Python 負のインデックス
似たようなことを C++ でやろうとすると次の様な感じ.
std::cout << a[a.size()-1] ;


この様な記述ができるからか,今まで負のインデックスの必要性は感じたことがなかった.
また配列の検索で該当するものがないと -1 を返す様なコードもよく書く.
インデックスを変数で持つ時は?
いろいろ考えると個人的にはあまり使わないのではないかと思う.
Python などをやればもっと有効な使い方が出てくるのか?

Is this 投稿 useful? Useful Useless 0 of 0 people say this 投稿 is useful.

コメントする

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

%d人のブロガーが「いいね」をつけました。