前言
先生の無線マイクが信号混乱かも、今日はサイレント授業をやった。
IoT知識
静音授業なので、魚を触っていた。すまない父親が払った学費/泣きかぶり
python
pythonプログラミング(3)
コンテナの種類
Markdownは行列の合併ができない為、以下の表はHTMLより作成
種類 | 要素へのアクセス方法 | mutable/ imutable |
特徴 |
---|---|---|---|
リスト | オフセットで位置を指定,値が含まれているかどうかも判定可能 | mutable | スタック(FIFO)としても使用可 |
タプル | imutable | 要素の値の変更,要素の追加・削除は不可 | |
辞書 | オフセットの代わりにキーを用いて指定 | mutable | キーとバリューを関連付けて格納連想記憶,ハッシュと類似 |
集合 | 集合に含まれているかどうかを判定 | キーのみを持つ辞書のようなもの要素の重複がなく順番もない |
リスト
一.リストの生成, リストへの変換
-
リストの生成
- 空リストの生成
x = list() print(x) # [] x = [] print(x) # []
- 空でないリストの生成
x = ["apple","orange","pear"] print(x) # ['apple','orange','pear'] x = ["2","0","1","9"] print(x) # ['2','0','1','9']
-
リストへの変換
- 文字列からリストへの変換
x = list("cat") print(x) # ['c','a','t'] x = ("Apple","Orange","Pear") #tuple y = list(x) print(y) # ['Apple','Orange','Pear']
二.インデックスを用いた要素の参照と変更
-
要素の参照
- インデックスを用いた参照
x = ["a","b","c","d","e","f"] print(x[0]) print(x[1]) print(x[2]) print(x[-1]) print(x[-2]) print(x[-3]) #a #b #c #f #e #d
-
リスト要素の値の変更
- インデックスを用いた値の変更
x = ["green","yellow","red"] x[0] = "oops" print(x) # ['oops', 'yellow', 'red']
-
サブシークエンスの参照
- スライス(インデックスを用いた範囲指定)
x = ["a","b","c","d","e","f"] print(x[:]) print(x[0:]) print(x[1:]) print(x[2:4]) print(x[-4:-2]) #['a', 'b', 'c', 'd', 'e', 'f'] #['a', 'c', 'e'] #['b', 'd', 'f'] #['b', 'e'] #['c', 'e'] #['c', 'f']
-
サブシークエンスの変更
- スライスを用いた値の変更
x = ["a","b","c","d","e","f"] x[2:4] = ["x","y"] print(x) # ['a', 'b', 'x', 'y', 'e', 'f'] x = ["a","b","c","d","e","f"] x[1::2] = ["x","y","z"] print(x) # ['a', 'x', 'c', 'y', 'e', 'z']
-
要素の有無の確認
- 演算子 in を用いた要素の有無の確認
x = ["green","yellow","red"] print("green" in x) print("black" not in x) # True # True
-
要素のインデックスの検索
- 関数 index() を用いた要素のインデックスの検索
x = ["a","b","c","d","e","f"] print(x.index("c")) print(x.index("f")) # 2 # 5 print(x.index("z")) # ValueError: 'z' is not in list
三.リストへの要素の追加と削除
-
末尾への要素の追加
- メソッドppend()
x = [3,1,4] print(x) x.append(1) print(x) x.append(5) print(x) # [3, 1, 4] # [3, 1, 4, 1] # [3, 1, 4, 1, 5]
2.要素の取り出し
- メソッド pop() 末尾の要素の取り出し
-
メソッド pop(i) インデックスを用いた要素の取り出し
x = ["a","b","c"] y = [] print("x = ",x) print("y = ",y) y.append(x.pop()) print("x = ",x) print("y = ",y) y.append(x.pop()) print("x = ",x) print("y = ",y) y.append(x.pop()) print("x = ",x) print("y = ",y) # x = ['a', 'b', 'c'] # y = [] # x = ['a', 'b'] # y = ['c'] # x = ['a'] # y = ['c', 'b'] # x = [] # y = ['c', 'b', 'a']
四.リストに対する操作と関数
-
リストの連接
- +演算子
x = ["a","b","c"] y = ["d","e","f"] z = x + y print(z) # ['a', 'b', 'c', 'd', 'e', 'f'] x = ["a","b","c"] y = ["d","e","f"] x += y print(x) # ['a', 'b', 'c', 'd', 'e', 'f']
- メソッド append() の違い
x = ["a","b","c"] y = ["d","e","f"] x.append(y) print(x) # ['a', 'b', 'c', ['d', 'e', 'f']]
- メソッド extend()
x = ["a","b","c"] y = ["d","e","f"] x.extend(y) print(x) # ['a', 'b', 'c', 'd', 'e', 'f']
-
要素の削除
- del 文を使用
x = ["a","b","c"] del x[1] print(x) # ['a', 'c'] x = ["a","b","c"] del x[-1] print(x) # ['a','b']
- メソッド remove()
x = ["a","b","c"] x.remove["b"] print(x) # ['a', 'c']
-
リストの長さ(要素数)
- 関数 len()
x = ["a","b","c"] n = len(x) print(n) # 3
- 関数 count()
x = list("abc") print(x) n = x.count("b") print(n) # ["a","b","c"] # 2 x = "abc" print(x) n = x.count("b") print(n) # abc # 2
五.リストの代入とコピー
-
リストの代入
- = による変数への代入
x = ["green","yellow","red"] y = x print("x = ",x) print("y = ",y) # x = ['green', 'yellow', 'red'] # y = ['green', 'yellow', 'red']
- 元のリストの要素の修 正結果が代入先にも反 映される
x = ["green","yellow","red"] y = x print("x = ",x) print("y = ",y) x[0] = "oops" print("x = ",x) print("y = ",y) # x = ['green', 'yellow', 'red'] # y = ['green', 'yellow', 'red'] # x = ['oops', 'yellow', 'red'] # y = ['oops', 'yellow', 'red']
-
関数 copy()
- copy()を用いると別のオブジェクトが生成される
x = ["green","yellow","red"] y = x.copy() print("x = ",x) print("y = ",y) x[0] = "oops" print("x = ",x) print("y = ",y) # x = ['green', 'yellow', 'red'] # y = ['green', 'yellow', 'red'] # x = ['oops', 'yellow', 'red'] # y = ['green', 'yellow', 'red']
-
list()変換関数
- リストのスライス [:]
x = ["green","yellow","red"] y = x[:] print("x = ",x) print("y = ",y) x[0] = "oops" print("x = ",x) print("y = ",y) # x = ['green', 'yellow', 'red'] # y = ['green', 'yellow', 'red'] # x = ['oops', 'yellow', 'red'] # y = ['green', 'yellow', 'red']
演習課題
略
# 略
質問
何か質問が有ったら、コメントしてください。
多分何も回答しない(笑)、
何故なら?この部落のURLは自分しか知ってる人間がいないので、
自分を自分に質問など、
面白いかな?
未来の私が今の私にコメントする事(笑い
以上