これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。
HTML ファイル生成日時: 2024/12/21 11:44:57.596 (台灣標準時)
Python には sets というデータ構造が用意されているのでござるが、これま で使っていなかったので、使い方を調べてみたでござる。
まず、以下のようなコードを用意してみたでござる。
#!/usr/pkg/bin/python3.9 # # Time-stamp: <2022/09/01 17:54:24 (CST) daisuke> # # making sets sets_cities = { 'Taipei', 'Keelung', 'Taoyuan', 'Hsinchu', 'Taichung', \ 'Chiayi', 'Tainan', 'Kaohsiung', 'Pingtung', 'Taitung', \ 'Hualien', 'Yilan', \ } # checking type of sets_cities type_sets_cities = type (sets_cities) print ("type of sets_cities:", type_sets_cities) # printing sets_cities print ("sets_cities:", sets_cities) # checking number of elements in sets_cities len_sets_cities = len (sets_cities) print ("number of elements:", len_sets_cities)
実行結果は以下の通りでござる。
% ./test_sets_01.py type of sets_cities:
sets_cities: {'Keelung', 'Taitung', 'Taipei', 'Taichung', 'Yilan', 'Hsinchu', 'Kaohsiung', 'Chiayi', 'Pingtung', 'Hualien', 'Taoyuan', 'Tainan'} number of elements: 12
|
---|
|
重複する要素があるデータから set を作ると、重複は取り除かれるでござる。
#!/usr/pkg/bin/python3.9 # # Time-stamp: <2022/09/01 17:55:04 (CST) daisuke> # # making sets from data with duplicates sets_cities = { 'Taipei', 'Keelung', 'Taoyuan', 'Hsinchu', 'Taichung', \ 'Chiayi', 'Tainan', 'Kaohsiung', 'Pingtung', 'Taitung', \ 'Hualien', 'Yilan', 'Taipei', 'Taichung', 'Tainan', \ } # checking type of sets_cities type_sets_cities = type (sets_cities) print ("type of sets_cities:", type_sets_cities) # printing sets_cities print ("sets_cities:", sets_cities) # checking number of elements in sets_cities len_sets_cities = len (sets_cities) print ("number of elements:", len_sets_cities)
% ./test_sets_02.py type of sets_cities:
sets_cities: {'Chiayi', 'Taoyuan', 'Yilan', 'Keelung', 'Hualien', 'Hsinchu', 'Taipei', 'Tainan', 'Pingtung', 'Taichung', 'Kaohsiung', 'Taitung'} number of elements: 12
|
---|
|
リストから set を作ってみるでござる。
#!/usr/pkg/bin/python3.9 # # Time-stamp: <2022/09/01 17:55:47 (CST) daisuke> # # a list list_cities = [ 'Taipei', 'Keelung', 'Taoyuan', 'Hsinchu', 'Taichung', \ 'Chiayi', 'Tainan', 'Kaohsiung', 'Pingtung', 'Taitung', \ 'Hualien', 'Yilan', 'Taipei', 'Taichung', 'Tainan', \ ] # making sets from a list sets_cities = set (list_cities) # checking type of list_cities type_list_cities = type (list_cities) print ("type of list_cities:", type_list_cities) # checking type of sets_cities type_sets_cities = type (sets_cities) print ("type of sets_cities:", type_sets_cities) # printing list_cities and sets_cities print ("list_cities:", list_cities) print ("sets_cities:", sets_cities) # checking number of elements in list_cities len_list_cities = len (list_cities) print ("number of elements in list:", len_list_cities) # checking number of elements in sets_cities len_sets_cities = len (sets_cities) print ("number of elements in sets:", len_sets_cities)
% ./test_sets_03.py type of list_cities:
type of sets_cities: list_cities: ['Taipei', 'Keelung', 'Taoyuan', 'Hsinchu', 'Taichung', 'Chiayi', 'Tainan', 'Kaohsiung', 'Pingtung', 'Taitung', 'Hualien', 'Yilan', 'Taipei', 'Taichung', 'Tainan'] sets_cities: {'Kaohsiung', 'Taoyuan', 'Chiayi', 'Pingtung', 'Hualien', 'Keelung', 'Hsinchu', 'Taipei', 'Taichung', 'Tainan', 'Yilan', 'Taitung'} number of elements in list: 15 number of elements in sets: 12
|
---|
|
set に要素を追加する方法は以下の通りのようでござる。
#!/usr/pkg/bin/python3.9 # # Time-stamp: <2022/09/01 17:56:41 (CST) daisuke> # # a list list_cities = [ 'Taipei', 'Keelung', 'Taoyuan', 'Hsinchu', 'Taichung', \ 'Chiayi', 'Tainan', 'Kaohsiung', 'Pingtung', 'Taitung', \ 'Hualien', 'Yilan', 'Taipei', 'Taichung', 'Tainan', \ ] # making sets from a list sets_cities = set (list_cities) # checking type of sets_cities type_sets_cities = type (sets_cities) print ("type of sets_cities:", type_sets_cities) # printing sets_cities print ("sets_cities:", sets_cities) # checking number of elements in sets_cities len_sets_cities = len (sets_cities) print ("number of elements in sets:", len_sets_cities) # adding more data to sets sets_cities.add ('Miaoli') sets_cities.add ('Nantou') sets_cities.add ('Yunlin') # printing sets_cities print ("sets_cities:", sets_cities) # checking number of elements in sets_cities len_sets_cities = len (sets_cities) print ("number of elements in sets:", len_sets_cities) # adding an existing element to sets sets_cities.add ('Taipei') # printing sets_cities print ("sets_cities:", sets_cities) # checking number of elements in sets_cities len_sets_cities = len (sets_cities) print ("number of elements in sets:", len_sets_cities)
% ./test_sets_04.py type of sets_cities:
sets_cities: {'Keelung', 'Taitung', 'Tainan', 'Hsinchu', 'Taipei', 'Taichung', 'Taoyuan', 'Chiayi', 'Pingtung', 'Hualien', 'Kaohsiung', 'Yilan'} number of elements in sets: 12 sets_cities: {'Keelung', 'Taitung', 'Miaoli', 'Tainan', 'Hsinchu', 'Taipei', 'Taichung', 'Taoyuan', 'Chiayi', 'Pingtung', 'Hualien', 'Kaohsiung', 'Yunlin', 'Nantou', 'Yilan'} number of elements in sets: 15 sets_cities: {'Keelung', 'Taitung', 'Miaoli', 'Tainan', 'Hsinchu', 'Taipei', 'Taichung', 'Taoyuan', 'Chiayi', 'Pingtung', 'Hualien', 'Kaohsiung', 'Yunlin', 'Nantou', 'Yilan'} number of elements in sets: 15
|
---|
|
set の要素の存在確認は以下のようにするようでござる。
#!/usr/pkg/bin/python3.9 # # Time-stamp: <2022/09/01 17:57:17 (CST) daisuke> # # a list list_cities = [ 'Taipei', 'Keelung', 'Taoyuan', 'Hsinchu', 'Taichung', \ 'Chiayi', 'Tainan', 'Kaohsiung', 'Pingtung', 'Taitung', \ 'Hualien', 'Yilan', 'Taipei', 'Taichung', 'Tainan', \ ] # making sets from a list sets_cities = set (list_cities) # checking type of sets_cities type_sets_cities = type (sets_cities) print ("type of sets_cities:", type_sets_cities) # printing sets_cities print ("sets_cities:", sets_cities) # checking number of elements in sets_cities len_sets_cities = len (sets_cities) print ("number of elements in sets:", len_sets_cities) # existence check existence_taipei = 'Taipei' in sets_cities print ("Taipei in sets?", existence_taipei) # one more existence check existence_miaoli = 'Miaoli' in sets_cities print ("Miaoli in sets?", existence_miaoli)
% ./test_sets_05.py type of sets_cities:
sets_cities: {'Hsinchu', 'Taichung', 'Chiayi', 'Tainan', 'Kaohsiung', 'Pingtung', 'Taitung', 'Yilan', 'Keelung', 'Taoyuan', 'Hualien', 'Taipei'} number of elements in sets: 12 Taipei in sets? True Miaoli in sets? False
|
---|
|
二つの sets の AND と OR の演算は以下のようにするようでござる。
#!/usr/pkg/bin/python3.9 # # Time-stamp: <2022/09/01 17:57:24 (CST) daisuke> # # lists list_n1 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] list_n2 = [ 2, 4, 6, 8, 10 ] # making sets from lists sets_n1 = set (list_n1) sets_n2 = set (list_n2) # printing sets print ("n1:", sets_n1) print ("n2:", sets_n2) # AND sets_n1_and_n2 = sets_n1 & sets_n2 # printing n1 AND n2 print ("n1 AND n2:", sets_n1_and_n2) # OR sets_n1_or_n2 = sets_n1 | sets_n2 # printing n1 OR n2 print ("n1 OR n2:", sets_n1_or_n2)
% ./test_sets_06.py n1: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} n2: {2, 4, 6, 8, 10} n1 AND n2: {2, 4, 6, 8, 10} n1 OR n2: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
|
---|
|
一方の set にはあるが、もう一方の set にはない要素を探す方法は以下の通 りのようでござる。
#!/usr/pkg/bin/python3.9 # # Time-stamp: <2022/09/01 17:57:29 (CST) daisuke> # # lists list_n1 = [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ] list_n2 = [ 2, 4, 6, 8, 10, 12, 14, 16, 18, 20 ] # making sets from lists sets_n1 = set (list_n1) sets_n2 = set (list_n2) # printing sets print ("n1:", sets_n1) print ("n2:", sets_n2) # elements in n1 but not in n2 diff_n1_n2 = sets_n1 - sets_n2 # printing elements in n1 but not in n2 print ("in n1, but not in n2:", diff_n1_n2)
% ./test_sets_07.py n1: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} n2: {2, 4, 6, 8, 10, 12, 14, 16, 18, 20} in n1, but not in n2: {1, 3, 5, 7, 9}
|
---|
|
set に対する for ループは以下のようにすればよいようでござる。
#!/usr/pkg/bin/python3.9 # # Time-stamp: <2022/09/01 17:57:39 (CST) daisuke> # # making sets sets_cities = { 'Taipei', 'Keelung', 'Taoyuan', 'Hsinchu', 'Taichung', \ 'Chiayi', 'Tainan', 'Kaohsiung', 'Pingtung', 'Taitung', \ 'Hualien', 'Yilan', \ } # printing sets print ("sets_cities:", sets_cities) # for loop for sets print ("elements in sets_cities:") for city in sets_cities: print (" %s" % city)
% ./test_sets_08.py sets_cities: {'Taichung', 'Kaohsiung', 'Hualien', 'Hsinchu', 'Taipei', 'Yilan', 'Keelung', 'Chiayi', 'Taitung', 'Tainan', 'Taoyuan', 'Pingtung'} elements in sets_cities: Taichung Kaohsiung Hualien Hsinchu Taipei Yilan Keelung Chiayi Taitung Tainan Taoyuan Pingtung
|
---|
|