リストオブジェクトは「[要素, 要素, …]」の形で作成します。
#例
lst = ['python', 'perl', 'php']
#空のリスト
lst = []
要素を取得するには「リストオブジェクト[位置]」を使います。
位置は、先頭の要素が0、二番目の要素が1となります。
負の値を指定した場合は、末尾から数えます。
範囲外を指定した場合はIndexErrorの例外が送出されます。
>>> lst = ['python', 'perl', 'php']
>>> lst[0]
'python'
>>> lst[1]
'perl'
>>> lst[2]
'php'
>>> lst[-1]
'php'
>>> lst[3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
「リストオブジェクト[start:end:step」」でリストの部分要素のリストを取得できます。
スライスと呼びます。
startは部分要素の開始位置を指定します。
省略した場合は0となります。
endは部分要素の終了位置を指定します。
省略した場合はリスト要素数と同じになります。
stepはステップ数です。
省略した場合は1となります。
位置には負の数も使用できます。
>>> lst
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> lst[2:] #lst[2:10:1]と同じ
[2, 3, 4, 5, 6, 7, 8, 9]
>>> lst[2:8] #lst[2:8:1]と同じ
[2, 3, 4, 5, 6, 7]
>>> lst[:8] #lst[0:8:1]と同じ
[0, 1, 2, 3, 4, 5, 6, 7]
>>> lst[2:8:2]
[2, 4, 6]
>>> lst[5::-1]
[5, 4, 3, 2, 1, 0]
「リストオブジェクト.sppend(要素)]でリストの末尾に要素を追加します。
>>> lst
['python', 'perl', 'php']
>>> lst.append('ruby')
>>> lst
['python', 'perl', 'php', 'ruby']
指定した位置に要素を追加するには「リストオブジェクト.insert(位置, 要素)」を使用します。
位置は、先頭の要素が0、二番目の要素が1となります。
>>> lst
['python', 'perl', 'php', 'ruby']
>>> lst = ['python', 'perl', 'php']
>>> lst.insert(0, 'ruby')
>>> lst
['ruby', 'python', 'perl', 'php']
リストオブジェクトの末尾に他のリストオブジェクトの要素を追加するには「リストオブジェクト.extend(リストオブジェクト)」を使用します。
>>> lst = ['python', 'perl']
>>> lst.extend(['php', 'ruby'])
>>> lst
['python', 'perl', 'php', 'ruby']
要素を削除するには「リストオブジェクト.remove(値)」を使います。
リスト中で指定した値を持つ最初の要素を削除します。
該当する要素がなければ、ValueErrorを送出します。
>>> lst = ['python', 'perl', 'php']
>>> lst.remove('perl')
>>> lst
['python', 'php']
>>> lst.remove('ruby')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list
指定した位置の要素を削除するには「リストオブジェクト.pop(位置)」を使います。
位置を指定しなければ、末尾の要素を削除します。
削除した要素を返します。
>>> lst = ['python', 'perl', 'php']
>>> lst.pop(0)
'python'
>>> lst
['perl', 'php']
>>> lst.pop()
'php'
>>> lst
['perl']
「del(リストオブジェクト[位置])」でも、指定した位置の要素を削除できます。
del()は値を返しません。
>>> lst
['python', 'perl', 'php']
>>> del(lst[1])
>>> lst
['python', 'php']
「リストオブジェクト.index(値)」はリスト中で指定した値を持つ最初の要素の位置を返します。
該当する要素がなければ、ValueErrorを送出します。
>>> lst = ['python', 'perl', 'php']
>>> lst.index('perl')
1
>>> lst.index('ruby')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.index(x): x not in list
リストオブジェクトの要素の数は「len(リストオブジェクト)」で取得できます。
>>> lst = ['python', 'perl', 'php']
>>> len(lst)
3
リスト中に指定した値を持つ要素の数を数えるには「リストオブジェクト.count(値)」を使用します。
>>> lst = ['python','perl','php','python','python']
>>> lst.count('python')
3
>>> lst.count('perl')
1
指定した値と同じ要素があるかどうかを調べるには「値 in リストオブジェクト」を使用します。
「値 not in リストオブジェクト」は「not 値 in リストオブジェクト」と同じです。
>>> lst = ['python', 'perl', 'php']
>>> 'perl' in lst
True
>>> 'ruby' in lst
False
「min(リストオブジェクト)」はリスト中の最小の要素を返します。
>>> lst
['python', 'perl', 'php']
>>> min(lst)
'perl'
「max(リストオブジェクト)」はリスト中の最大の要素を返します。
>>> lst
['python', 'perl', 'php']
>>> max(lst)
'python'
リストの要素をソートするには「リストオブジェクト.sort()」を使用します。
>>> lst
['python', 'perl', 'php']
>>> lst.sort()
>>> lst
['perl', 'php', 'python']
逆順に並び替えるには「リストオブジェクト.reverse()」を使用します。
>>> lst
['python', 'perl', 'php']
>>> lst.reverse()
>>> lst
['php', 'perl', 'python']
「tuple(リスト)」はリストと同じ要素を同じ順番に持つタプルを返します。
>>> lst
['python', 'perl', 'php']
>>> tuple(lst)
('python', 'perl', 'php')
複製を作成するには「list(リストオブジェクト)」を使用します。
>>> lst
['python', 'perl', 'php']
>>> lst2 = list(lst)
>>> lst.append('ruby')
>>> lst
['python', 'perl', 'php', 'ruby']
>>> lst2
['python', 'perl', 'php']
条件に該当する要素を抽出するには「filter(関数, リストオブジェクト)」を使用します。
関数の結果が真になる要素からなるリストオブジェクトを返します。
>>> lst = [1,2,3,4,5]
>>> def f(x): return x % 2 == 0 #偶数なら真
...
>>> filter(f, lst)
[2, 4] #偶数のみ抽出した
「map(関数, リストオブジェクト)」はリストのすべての要素に関数を適用して、その返値からなるリストを返します。
>>> lst = [1,2,3,4,5]
>>> def f(x): return x * 2 #値を2倍にする
...
>>> map(f, lst)
[2, 4, 6, 8, 10] #2倍になった
「reduce(関数, リストオブジェクト)」は、引数を2つとる関数にリストの要素を引数と呼び出して、その結果とリストの次の要素を関数の引数とします。
>>> lst
[1, 2, 3, 4, 5]
>>> def f(x,y): return x + y #引数の和を返す
...
>>> reduce(f, lst)
15 #リストの要素の和
「sum(リストオブジェクト, start)」はstartを初期値として、リストの要素の総和を返します。
startを省略した場合は0になります。
>>> lst = [1,2,3,4,5]
>>> sum(lst)
15
>>> sum(lst, 10)
25
連続した数値の要素を持つリストオブジェクトの作成するには「range(start, stop, step)」を使用します。
startからstopまでstep-1ずつ増える要素を持つリストを返します。
startを省略した場合は1となります。
stepを省略した場合は1となります。
>>> range(5)
[0, 1, 2, 3, 4]
>>> range(5,10)
[5, 6, 7, 8, 9]
>>> range(10,20, 2)
[10, 12, 14, 16, 18]
複数のリストオブジェクトからリストを作成するには「zip(リストオブジェクト, …)」を使用します。
それぞれのリストの要素を並行して取得したタプルを返します。
取得数は一番要素の数が少ないリストオブジェクトの要素数と同じです。
>>> for v in zip([1,2,3],['a','b','c'],['A','B','C']):
... print v
...
(1, 'a', 'A')
(2, 'b', 'B')
(3, 'c', 'C')