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']
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']
Crieitは誰でも投稿できるサービスです。 是非記事の投稿をお願いします。どんな軽い内容でも投稿できます。
また、「こんな記事が読みたいけど見つからない!」という方は是非記事投稿リクエストボードへ!
こじんまりと作業ログやメモ、進捗を書き残しておきたい方はボード機能をご利用ください。
ボードとは?
コメント