2021-03-27に投稿

Python リスト型 list

Pythonのリスト型listは基本的なシーケンス型の1つでミュータブル(作成後に変更できるオブジェクト)です。

リストの定義

コンマ区切で要素を並べ、角括弧で囲む他、
内包表記やlistを使って定義できます。

list_sample =  []
list_sample =  ['a', 'b', 'c']
list_sample =  [x for x in 'xyz']
list_sample =  list([1, 2, 3])

リストの入れ子

m = [['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I']]
print(m[1]) # => ['D', 'E', 'F']]
print(m[1][2]) # =>  F

リスト情報の取得

リストの要素へのアクセス

インデックスやスライスでのアクセスが可能です。

list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample[0] # => a
list_sample[1] # => b
list_sample[-1] # => j
list_sample[2:5] # => ['c', 'd', 'e']
list_sample[3:9:2] # => ['d', 'f', 'h']
list_sample[:] # => ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

リストの長さ

list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
print(len(list_sample)) # => 10

回数カウント

list_sample =  ['a', 'b', 'a', 'c', 'a', 'c']
list_sample.count('a') # => 3

要素の存在判定

list_sample =  ['a', 'b', 'c']
'a' in list_sample # => True
'X' not in list_sample # => True

最大最小

list_sample =  [1, 5, 2, 4]
max(list_sample) # => 5
min(list_sample) # => 1

インデックスの位置

index(対象の要素[, 開始位置[, 終了位置]])で要素の位置を取得

list_sample =  ['a', 'b', 'a', 'b', 'c', 'a' , 'c']
list_sample.index('a')  # => 0
list_sample.index('a', 1)  # => 2
list_sample.index('a', 3 , 6)  # => 5

リストの操作

リスト要素の更新

list_sample =  ['a', 'b', 'c']
list_sample[1] = 'X'
list_sample # => ['a', 'X', 'c']

要素の追加

list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample.append('X')
print(list_sample) # => ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'X']

要素の挿入

list_sample =  ['a', 'b', 'c']
list_sample.insert(2,'X')
list_sample # => ['a', 'b', 'X', 'c']

指定した値と同じ最初の要素を除去

list_sample =  ['a', 'b', 'c', 'b', 'a']
list_sample.remove('b')
list_sample # => ['a', 'c', 'b', 'a']

指定位置の要素を取り出す

list_sample =  ['a', 'b', 'c']
list_sample.pop(1) # => 'b'
list_sample # => ['a', 'c']

delによる削除

list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
del list_sample[3:5]
list_sample # => ['a', 'b', 'c', 'f', 'g', 'h', 'i', 'j']
del list_sample[2:7:2]
list_sample # => ['a', 'b', 'f', 'h', 'j']

要素のクリア

list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample.clear()
list_sample # => []

リストのコピー

list_sample =  ['a', 'b', 'c']
list_copy = list_sample.copy()
list_copy # => ['a', 'b', 'c']

スライスによるリストの操作

list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample[2:4] = ['1', '2', '3']
print(list_sample) # => ['a', 'b', '1', '2', '3', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample[3:] = []
print(list_sample) # => ['a', 'b', '1']
list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample[2:7:2] = ['X', 'Y', 'Z']
list_sample # => ['a', 'b', 'X', 'd', 'Y', 'f', 'Z', 'h', 'i', 'j']

ソート

list_sample =  ['a', 'c', 'A', 'b', 'C', 'B' ]
list_sample.sort()
list_sample # => ['A', 'B', 'C', 'a', 'b', 'c']
list_sample.sort(key=str.lower)
list_sample # => ['a', 'A', 'b', 'B', 'c', 'C']
list_sample.sort(key=str.lower, reverse=True)
list_sample # => ['c', 'C', 'b', 'B', 'a', 'A']

順序逆転、逆順

list_sample =  ['a', 'b', 'c']
list_sample.reverse()
list_sample # => ['c', 'b', 'a']

リストの繰り返し

list_sample = [1, 2]
list_sample * 3 #=> [1, 2, 1, 2, 1, 2]
list_sample *= 5 #=> [1, 2, 1, 2, 1, 2, 1, 2, 1, 2]

リストの連結

+で末尾にリスト追加する。

list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample + ['1','2', '3']
# => ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', '1', '2', '3']
list_sample =  ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
list_sample.extend(['1','2', '3'])
list_sample # => ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', '1', '2', '3']
Originally published at marusankakusikaku.jp
ツイッターでシェア
みんなに共有、忘れないようにメモ

maru3kaku4kaku

Pythonこつこつ学習中。よく忘れる。

Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。

また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!

有料記事を販売できるようになりました!

こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?

コメント