【Python】 リファレンス list ミュータブルなシーケンス
シーケンスシリーズ、これが最後。
####################################
# ミュータブルなシーケンス
# ミュータブルとは変更可能であるということ
入れ換え
# l[i] = x : lのi番目の要素をxと入れ換え
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l[4] = 44
# l = [0, 1, 2, 3, 44, 5, 6, 7, 8, 9, 10]
------------------------------------
# l[i:j] = t :
# lのiからj-1番目をイテラブルtと入れ換え
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l[1:4] = [11, 22, 33]
# l = [0, 11, 22, 33, 4, 5, 6, 7, 8, 9, 10]
# l[i:] = t :
# lのi以降全部をイテラブルtと入れ換え
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l[1:] = [11, 22, 33]
# l = [0, 11, 22, 33]
# l[:j] = t :
# lのj-1番目までを全部イテラブルtと入れ換え
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l[:6] = [11, 22, 33]
# l = [11, 22, 33, 6, 7, 8, 9, 10]
------------------------------------
# l[i:j:k] = t :
# lのiからj-1番目をk毎にイテラブルtと入れ 換え
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l[1:6:2] = [111, 333, 555]
# l = [0, 111, 2, 333, 4, 555, 6, 7, 8, 9, 10]
# l[:j:k] = t :
# lのj-1番目までをk毎にイテラブルtと入れ換え
# tの要素の数があわないとエラー
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l[:6:2] = [111, 333, 555, 777]
# l = [111, 1, 333, 3, 555, 5, 777, 7, 8, 9, 10]
# l[i::k] = t :
# lのi以降をk毎にイテラブルtと入れ換え
# tの要素の数があわないとエラー
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l[3::2] = [111, 333, 555, 777]
# l = [0, 1, 2, 111, 4, 333, 6, 555, 8, 777, 10]
# l[i:j:] = t :
# lのiからj-1番目を1毎にイテラブルtと入れ 換え
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l[1:6:] = [111, 333, 555]
# l = [0, 111, 333, 555, 6, 7, 8, 9, 10]
del - 削除
------------------------------------
# del l[i:j] : lのiからj-1番目を削除
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del l[1:4]
# l = [0, 4, 5, 6, 7, 8, 9, 10]
# del l[:j] : lのj-1番目までを削除
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del l[:4]
# l = [4, 5, 6, 7, 8, 9, 10]
# del l[i:] : lのi以降を削除
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del l[3:]
# l = [0, 1, 2]
------------------------------------
# del l[i:j:k] : lのiからj-1番目をk毎に削除
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del l[1:6:2]
# l = [0, 2, 4, 6, 7, 8, 9, 10]
# del l[:j:k] : lのj-1番目までをk毎に削除
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del l[:6:2]
# l = [1, 3, 5, 6, 7, 8, 9, 10]
# del l[i::k] : lのi以降をk毎に削除
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del l[3::2]
# l = [0, 1, 2, 4, 6, 8, 10]
# del l[i:j:] : lのiからj-1番目を1毎に削除
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
del l[1:6:]
# l = [0, 6, 7, 8, 9, 10]
append - 末尾に追加
------------------------------------
# l.append(x) : lの末尾にxを追加
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l.append(16)
# l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 16]
clear - 全消去
# l.clear() : lの全要素削除
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l.clear()
# l = []
copy - コピー
# m = l.copy() : lをmにコピー
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
m = l.copy()
# l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# m = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
extend - 拡張
# l.extend(m) : lの末尾にmを拡張
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
m = [16, 17, 18]
l.extend(m)
# l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 17, 18]
# m = [16, 17, 18]
n倍
# l *= n : lをn倍
l = [0, 1, 2, 3]
l *= 3
# l = [0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]
insert - 挿入
# l.insert(i, x) : lのi番目にxを挿入
l = [0, 1, 2, 3, 4, 5]
l.insert(4, 44)
# l = [0, 1, 2, 3, 44, 4, 5]
pop - 取り出して取り除く
# n = l.pop() : lから要素を取り出して取り除く
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n = l.pop()
# l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
# n = 10
n = l.pop()
# l = [0, 1, 2, 3, 4, 5, 6, 7, 8]
# n = 9
# n = l.pop(i) : lからi番目の要素を取り出して 取り除く
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
n = l.pop(1)
# l = [0, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# n = 1
n = l.pop(1)
# l = [0, 3, 4, 5, 6, 7, 8, 9, 10]
# n = 2
remove - 指定要素削除
# l.remove(x):
# lの中でxが最初に出現する要素を取り除く
l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
l.remove(3)
# l = [0, 1, 2, 4, 5, 6, 7, 8, 9, 10]
reverse - 逆順
# l.reverse() :
# lをリバース
l = [0, 111, 2, 333, 4, 555, 6, 7, 8, 9, 10]
l.reverse()
# l = [10, 9, 8, 7, 6, 555, 4, 333, 2, 111, 0]
sort - 並べ替え
------------------------------------
# l.sort() :
# lを並べ替え
l = [0, 111, 2, 333, 4, 555, 6, 7, 8, 9, 10]
l.sort()
# l = [0, 2, 4, 6, 7, 8, 9, 10, 111, 333, 555]
# アルファベットは大文字が先に並べられる
l = ['banana', 'Orange', 'Apple']
l.sort()
# l = ['Apple', 'Orange', 'banana']
# 「str.lower」を指定すれば全部小文字で比 較される
l.sort(key=str.lower)
# l = ['Apple', 'banana', 'Orange']
# 「reverse=True」を指定すれば逆順に並ぶ
l.sort(key=str.lower, reverse=True)
# l = ['Orange', 'banana', 'Apple']
def tmp(str):
print('# tmp ', str)
return str
# 「key」にはユーザ定義関数も使用できる
l.sort(key=tmp)
# tmp Orange
# tmp banana
# tmp Apple
# l = ['Apple', 'Orange', 'banana']
コード
def tmp(str):
print('# tmp ', str)
return str
print('####################################')
print('# ミュータブルなシーケンス')
print('# ミュータブルとは変更可能であるということ')
print()
print('# l[i] = x : lのi番目の要素をxと入れ換え')
l = [x for x in range(11)]
print('l =', l)
l[4] = 44
print('l[4] = 44')
print('# l =', l)
print()
print('------------------------------------')
print('# l[i:j] = t :')
print('# lのiからj-1番目をイテラブルtと入れ換え')
l = [x for x in range(11)]
print('l =', l)
l[1:4] = [11, 22, 33]
print('l[1:4] = [11, 22, 33]')
print('# l =', l)
print()
print('# l[i:] = t :')
print('# lのi以降全部をイテラブルtと入れ換え')
l = [x for x in range(11)]
print('l =', l)
l[1:] = [11, 22, 33]
print('l[1:] = [11, 22, 33]')
print('# l =', l)
print()
print('# l[:j] = t :')
print('# lのj-1番目までを全部イテラブルtと入れ換え')
l = [x for x in range(11)]
print('l =', l)
l[:6] = [11, 22, 33]
print('l[:6] = [11, 22, 33]')
print('# l =', l)
print()
print('------------------------------------')
print('# l[i:j:k] = t :')
print('# lのiからj-1番目をk毎にイテラブルtと入れ換え')
l = [x for x in range(11)]
print('l =', l)
l[1:6:2] = [111, 333, 555]
print('l[1:6:2] = [111, 333, 555]')
print('# l =', l)
print()
print('# l[:j:k] = t :')
print('# lのj-1番目までをk毎にイテラブルtと入れ換え')
print('# tの要素の数があわないとエラー')
l = [x for x in range(11)]
print('l =', l)
l[:8:2] = [111, 333, 555, 777]
print('l[:6:2] = [111, 333, 555, 777]')
print('# l =', l)
print()
print('# l[i::k] = t :')
print('# lのi以降をk毎にイテラブルtと入れ換え')
print('# tの要素の数があわないとエラー')
l = [x for x in range(11)]
print('l =', l)
l[3::2] = [111, 333, 555, 777]
print('l[3::2] = [111, 333, 555, 777]')
print('# l =', l)
print()
print('# l[i:j:] = t :')
print('# lのiからj-1番目を1毎にイテラブルtと入れ換え')
l = [x for x in range(11)]
print('l =', l)
l[1:6:] = [111, 333, 555]
print('l[1:6:] = [111, 333, 555]')
print('# l =', l)
print()
print('------------------------------------')
print('# del l[i:j] : lのiからj-1番目を削除')
l = [x for x in range(11)]
print('l =', l)
del l[1:4]
print('del l[1:4]')
print('# l =', l)
print()
print('# del l[:j] : lのj-1番目までを削除')
l = [x for x in range(11)]
print('l =', l)
del l[:4]
print('del l[:4]')
print('# l =', l)
print()
print('# del l[i:] : lのi以降を削除')
l = [x for x in range(11)]
print('l =', l)
del l[3:]
print('del l[3:]')
print('# l =', l)
print()
print('------------------------------------')
print('# del l[i:j:k] : lのiからj-1番目をk毎に削除')
l = [x for x in range(11)]
print('l =', l)
del l[1:6:2]
print('del l[1:6:2]')
print('# l =', l)
print()
print('# del l[:j:k] : lのj-1番目までをk毎に削除')
l = [x for x in range(11)]
print('l =', l)
del l[:6:2]
print('del l[:6:2]')
print('# l =', l)
print()
print('# del l[i::k] : lのi以降をk毎に削除')
l = [x for x in range(11)]
print('l =', l)
del l[3::2]
print('del l[3::2]')
print('# l =', l)
print()
print('# del l[i:j:] : lのiからj-1番目を1毎に削除')
l = [x for x in range(11)]
print('l =', l)
del l[1:6:]
print('del l[1:6:]')
print('# l =', l)
print()
print('------------------------------------')
print('# l.append(x) : lの末尾にxを追加')
l = [x for x in range(11)]
print('l =', l)
l.append(16)
print('l.append(16)')
print('# l =', l)
print()
print('# l.clear() : lの全要素削除')
l = [x for x in range(11)]
print('l =', l)
l.clear()
print('l.clear()')
print('# l =', l)
print()
print('# m = l.copy() : lをmにコピー')
l = [x for x in range(11)]
print('l =', l)
m = l.copy()
print('m = l.copy()')
print('# l =', l)
print('# m =', m)
print()
print('# l.extend(m) : lの末尾にmを拡張')
l = [x for x in range(11)]
m = [16, 17, 18]
print('l =', l)
print('m =', m)
l.extend(m)
print('l.extend(m)')
print('# l =', l)
print('# m =', m)
print()
print('# l *= n : lをn倍')
l = [0, 1, 2, 3]
print('l =', l)
l *= 3
print('l *= 3')
print('# l =', l)
print()
print('# l.insert(i, x) : lのi番目にxを挿入')
l = [0, 1, 2, 3, 4, 5]
print('l =', l)
l.insert(4, 44)
print('l.insert(4, 44)')
print('# l =', l)
print()
print('# n = l.pop() : lから要素を取り出して取り除く')
l = [x for x in range(11)]
print('l =', l)
n = l.pop()
print('n = l.pop()')
print('# l =', l)
print('# n =', n)
n = l.pop()
print('n = l.pop()')
print('# l =', l)
print('# n =', n)
print()
print('# n = l.pop(i) : lからi番目の要素を取り出して取り除く')
l = [x for x in range(11)]
print('l =', l)
n = l.pop(1)
print('n = l.pop(1)')
print('# l =', l)
print('# n =', n)
n = l.pop(1)
print('n = l.pop(1)')
print('# l =', l)
print('# n =', n)
print()
print('# l.remove(x):')
print('# lの中でxが最初に出現する要素を取り除く')
l = [x for x in range(11)]
print('l =', l)
l.remove(3)
print('l.remove(3)')
print('# l =', l)
print()
print('# l.reverse() :')
print('# lをリバース')
l = [x for x in range(11)]
l[1:6:2] = [111, 333, 555]
print('l =', l)
l.reverse()
print('l.reverse()')
print('# l =', l)
print()
print('------------------------------------')
print('# l.sort() :')
print('# lを並べ替え')
l = [x for x in range(11)]
l[1:6:2] = [111, 333, 555]
print('l =', l)
l.sort()
print('l.sort()')
print('# l =', l)
print()
print('# アルファベットは大文字が先に並べられる')
l = ['banana', 'Orange', 'Apple']
print('l =', l)
l.sort()
print('l.sort()')
print('# l =', l)
print()
print('# 「str.lower」を指定すれば全部小文字で比較される')
l.sort(key=str.lower)
print('l.sort(key=str.lower)')
print('# l =', l)
print()
print('# 「reverse=True」を指定すれば逆順に並ぶ')
l.sort(key=str.lower, reverse=True)
print('l.sort(key=str.lower, reverse=True)')
print('# l =', l)
print()
print('def tmp(str):')
print(" print('# tmp ', str)")
print(' return str')
print('# 「key」にはユーザ定義関数も使用できる')
print('l.sort(key=tmp)')
l.sort(key=tmp)
print('# l =', l)
print()
この記事が気に入ったらサポートをしてみませんか?