Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

Pythonのリストについて

こんにちは、鯨井貴博@opensourcetechです。

 

Pythonのリストに関するメモです。

 

リストの作成

bash-3.2$ python
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> [1,2,3,4,5,6,7]・・・・数字のリスト
[1, 2, 3, 4, 5, 6, 7]
>>> ["a","b","c","d","e"]・・・文字列のリスト
['a', 'b', 'c', 'd', 'e']

 

リスト内の情報の数え方

bash-3.2$ python
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> [1,2,3,4,5,6,7]・・・・数字のリスト
[1, 2, 3, 4, 5, 6, 7]

Pythonでは、

リストの先頭から、0番目・1番目・2番目・・・・と数えていきます。

つまり上記の例では、

1が0番目・2が1番目・3が2番目・・・となり、

以下のような操作をすることで動作か確認できます。

 

リスト内情報の操作

bash-3.2$ python
Python 2.7.10 (default, Oct 6 2017, 22:29:07)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> [1,2,3,4,5,6,7]・・・・数字のリスト作成
[1, 2, 3, 4, 5, 6, 7]
>>> type([1,2,3,4,5,6,7])・・・・型の確認
<type 'list'>
>>> [1,2,3,4,5,6,7][0]・・・・0(リストの1番目)を取得
1
>>> [1,2,3,4,5,6,7][1]・・・・1(リストの2番目)を取得
2
>>> [1,2,3,4,5,6,7][2]・・・・2(リストの3番目)を取得
3
>>> [1,2,3,4,5,6,7][6]・・・・6(リストの7番目)を取得
7
>>> [1,2,3,4,5,6,7][1:]・・・・1:(リストの2番目以降)を取得
[2, 3, 4, 5, 6, 7]
>>> [1,2,3,4,5,6,7][2:]・・・・2:(リストの3番目以降)を取得
[3, 4, 5, 6, 7]
>>> [1,2,3,4,5,6,7][6:]・・・・6:(リストの7番目以降)を取得
[7]
>>> [1,2,3,4,5,6,7][:1]・・・・:1(リストの2番目より前のもの)を取得
[1]
>>> [1,2,3,4,5,6,7][:2]・・・・:2(リストの3番目より前のもの)を取得
[1, 2]
>>> [1,2,3,4,5,6,7][:6]・・・・:6(リストの7番目より前のもの)を取得
[1, 2, 3, 4, 5, 6]
>>> [1,2,3,4,5,6,7][1:3]・・・・1:3(リストの2番目以降、かつ4番目より前のもの)を取得
[2, 3]
>>> [1,2,3,4,5,6,7][1:4]・・・・1:4(リストの2番目以降、かつ5番目より前のもの)を取得
[2, 3, 4]
>>> ["a","b","c","d","e"]・・・・文字列のリスト作成
['a', 'b', 'c', 'd', 'e']
>>> ["a","b","c","d","e"][0]・・・・0(リストの1番目)を取得
'a'

 

 

 

www.slideshare.net

github.com

www.facebook.com

twitter.com

www.instagram.com

 

 

にほんブログ村 IT技術ブログ Linuxへ
Linux

にほんブログ村 IT技術ブログ オープンソースへ
オープンソース

 

 

Opensourcetech by Takahiro Kujirai