【Python超入門】リスト、辞書、【Part. 6】

Python

リスト、辞書

複数のデータを扱うことができるデータ型としてリスト、辞書がよくつかわれます。
本記事ではリスト、辞書の基本的な操作について解説します。

リスト

リストはデータの集まりです。
リストに対しては下記のような操作を行うことができます

演算解説
x in listlistにxと等しい要素が存在すればTrue、そうでなければFalse
x not in listlistにxと等しい要素が存在すればFalse、そうでなければTrue
list1 + list2list1とlist2を結合したリストを返す
list1 * nlist1をn回結合したリストを返す
list1[i]list1のi番目の値を返却します
list1[i:j]list1のi番目からj番目の値を返却します
list1[i:j:k]list1のi番目からj番目の値をkごとに返却します
len(list1)list1のデータサイズを返却します
min(list1)list1の中で最小の値を返却します
max(list1)list1の中で最大の値を返却します
list1.index(x[, i[, j]])list1の中でxが出現する最初のインデックスを返却します
(i、jを指定した場合、i、jの範囲のみに適用される)
list1.count(x)list1の中でxが出現する回数を返却します
list1[i] = xlist1のi番目の要素をxに変更します
list1[i:j] = tlist1のi番目からj番目の要素をxに変更します
del list1[i:j]list1のi番目からj番目の要素を削除します
list1[i:j:k] = tlist1のi番目からj番目の要素をk個ごとにxに変更します
del list1[i:j:k]list1のi番目からj番目の要素をk個ごとに削除します
list1.append(x)list1の最後にxを加えます
list1.clear()list1内の要素をすべてを削除します
list1.copy()list1をコピーします
list1.extend(list2)list1にlist2を連結します
list1 *= nlist1をn回繰り返したリストを返します
list1.insert(i, x)list1のi番目にxを挿入します
list1.pop()list1の最後の要素を取り出します。
list1.remove(x)list1からxと同じ要素で最初のものを取り除きます
list1.reverse()list1の要素を逆転させます
演算解説
x in listlistにxと等しい要素が存在すればTrue、そうでなければFalse
list1[i]list1のi番目の値を返却します
list1[i:j]list1のi番目からj番目の値を返却します
len(list1)list1のデータサイズを返却します
min(list1)list1の中で最小の値を返却します
max(list1)list1の中で最大の値を返却します
list1[i] = xlist1のi番目の要素をxに変更します
del list1[i:j]list1のi番目からj番目の要素を削除します
list1.append(x)list1の最後にxを加えます
list1.clear()list1内の要素をすべてを削除します
list1.extend(list2)list1にlist2を連結します
list1.insert(i, x)list1のi番目にxを挿入します
list1.pop()list1の最後の要素を取り出します。
list1.remove(x)list1からxと同じ要素で最初のものを取り除きます

以下、サンプルコードとその結果です

list1 = [1, 2, 3, 4, 5]   
list2 = [6, 7, 8, 9, 10]  
print(1 in list1)     # True
print(1 not in list1) # False
print(list1 + list2) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print(list1 * 3) # [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
print(list1[0]) # 1
print(list1[0:2]) # [1, 2]
print(list1[0:4:2]) # [1, 3]
print(len(list1)) # 5
print(min(list1)) # 1
print(max(list1)) # 5
print(list1.index(1)) # 0
print(list1.count(2)) # 1

list1 = [1, 2, 3, 4, 5]
list1[2] = 30
print(list1) # [1, 2, 30, 4, 5]

list1 = [1, 2, 3, 4, 5]
list1[2:4] = [30, 40]
print(list1) # [1, 2, 30, 40, 5]

list1 = [1, 2, 3, 4, 5]
del list1[2:4]
print(list1) # [1, 2, 5]

list1 = [1, 2, 3, 4, 5]
list1[0:5:2] = [10, 30, 50]
print(list1) # [10, 2, 30, 4, 50]

list1 = [1, 2, 3, 4, 5]
del list1[0:5:2]
print(list1) # [2, 4]

list1 = [1, 2, 3, 4, 5]
list1.append(6)
print(list1) # [1, 2, 3, 4, 5, 6]

list1 = [1, 2, 3, 4, 5]
list1.append(6)
list1.clear()
print(list1) # []

list1 = [1, 2, 3, 4, 5]
list2 = list1.copy()
list2.append(6)
print(list1, list2) # [1, 2, 3, 4, 5] [1, 2, 3, 4, 5, 6]

list1 = [1, 2, 3, 4, 5]
list2 = [6, 7, 8, 9, 10]
list1.extend(list2)
print(list1) # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

list1 = [1, 2, 3, 4, 5]
list1 *= 3
print(list1) # [1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

list1 = [1, 2, 3, 4, 5]
list1.insert(2, 20)
print(list1) # [1, 2, 20, 3, 4, 5]

list1 = [1, 2, 3, 4, 5]
list1.pop()
print(list1) # [1, 2, 3, 4]

list1 = [1, 2, 3, 4, 5]
list1.remove(4) 
print(list1) # [1, 2, 3, 5]

list1 = [1, 2, 3, 4, 5]
list1.reverse()
print(list1) # [5, 4, 3, 2, 1]

辞書

辞書はkeyとvalueの組み合わせを保持しております
辞書に対しては下記のような操作を行うことができます

演算解説
list(dict1)辞書のすべてのキーのリストを返却します
len(dict1)辞書の要素数を返却します
dict1[key]辞書のkeyに該当するvalueを返却します
dict1[key] = valuedict1[key]にvalueを設定します
del dict1[key]dict1[key]にvalueを削除します
key in dict1dict1にkeyがあればTrue、そうでなければFalseを返却します
key not in dict1dict1にkeyがあればFalse、そうでなければTrueを返却します
iter(dict1)dict1のキーのイテレータを返却します
dict1.clear()dict1の要素をすべて削除します
dict1.copy()dict1をコピーします
dict1.get(key[, default])dict1からkeyに該当するものを取得します。該当するkeyがない場合、defaultを返却します
dict1.items()dict1の項目(key, value)のビューを返却します
dict1.keys()dict1のキーのビューを返却します
dict1.pop(key[, default])dict1からkeyに該当するものを取り出します
dict1.popitem()dict1に直近追加したデータを取り出し
reversed(dict1)順序を逆転させたdict1のイテレータを返却します
dict1.update([other])dict1の要素をotherで更新します
dict1.values()dict1のvalueのビューを返却します

以下、サンプルコードとその結果です

dict1 = {"a":1, "b":2, "c":3, "d":4}
print(list(dict1)) # ['a', 'b', 'c', 'd']
print(len(dict1)) # 4
print(dict1["b"]) # 2

dict1 = {"a":1, "b":2, "c":3, "d":4}
dict1["e"] = 5
print(dict1) # {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

dict1 = {"a":1, "b":2, "c":3, "d":4}
del dict1["b"]
print(dict1) # {'a': 1, 'c': 3, 'd': 4}

dict1 = {"a":1, "b":2, "c":3, "d":4}
print("b" in dict1) # True

dict1 = {"a":1, "b":2, "c":3, "d":4}
print("b" not in dict1) # False

dict1 = {"a":1, "b":2, "c":3, "d":4}
print(iter(dict1)) # <dict_keyiterator object at 0x00000280C667FBD0>

dict1 = {"a":1, "b":2, "c":3, "d":4}
dict1.clear()
print(dict1) # {}

dict1 = {"a":1, "b":2, "c":3, "d":4}
dict2 = dict1.copy()
dict2["e"] = 5
print(dict1, dict2) # {'a': 1, 'b': 2, 'c': 3, 'd': 4} {'a': 1, 'b': 2, 'c': 3, 'd': 4, 'e': 5}

dict1 = {"a":1, "b":2, "c":3, "d":4}
print(dict1.get("c")) # 3

dict1 = {"a":1, "b":2, "c":3, "d":4}
print(dict1.items()) # dict_items([('a', 1), ('b', 2), ('c', 3), ('d', 4)])

dict1 = {"a":1, "b":2, "c":3, "d":4}
print(dict1.keys()) # dict_keys(['a', 'b', 'c', 'd'])

dict1 = {"a":1, "b":2, "c":3, "d":4}
dict1.pop("d")
print(dict1) # {'a': 1, 'b': 2, 'c': 3}

dict1 = {"a":1, "b":2, "c":3, "d":4}
print(dict1.popitem()) # ('d', 4)

dict1 = {"a":1, "b":2, "c":3, "d":4}
print(reversed(dict1)) # <dict_reversekeyiterator object at 0x00000280C668D040>

dict1 = {"a":1, "b":2, "c":3, "d":4}
dict1.update(d=40)
print(dict1) # {'a': 1, 'b': 2, 'c': 3, 'd': 40}

dict1 = {"a":1, "b":2, "c":3, "d":4}
dict1.update(d=40)
print(dict1.values()) # dict_values([1, 2, 3, 40])

参考
https://docs.python.org/ja/3/library/stdtypes.html#lists https://docs.python.org/ja/3/library/stdtypes.html#typesseq-common https://docs.python.org/ja/3/library/stdtypes.html#typesseq-mutable

タイトルとURLをコピーしました