Notebook

これは日々の作業を通して学んだことや毎日の生活で気づいたことをを記録しておく備忘録である。

HTML ファイル生成日時: 2024/12/21 11:44:57.596 (台灣標準時)

Python の sets について

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

fig_202209/python_sets_emacs_01.png
fig_202209/python_sets_xterm_01.png

重複する要素があるデータから 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

fig_202209/python_sets_emacs_02.png
fig_202209/python_sets_xterm_02.png

リストから 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

fig_202209/python_sets_emacs_03.png
fig_202209/python_sets_xterm_03.png

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

fig_202209/python_sets_emacs_04.png
fig_202209/python_sets_xterm_04.png

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

fig_202209/python_sets_emacs_05.png
fig_202209/python_sets_xterm_05.png

二つの 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}

fig_202209/python_sets_emacs_06.png
fig_202209/python_sets_xterm_06.png

一方の 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}

fig_202209/python_sets_emacs_07.png
fig_202209/python_sets_xterm_07.png

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

fig_202209/python_sets_emacs_08.png
fig_202209/python_sets_xterm_08.png


Frequently accessed files

  1. Computer___Python/20220518_0.html
  2. Computer___Network/20230726_00.html
  3. Misc___Taiwan/20240207_00.html
  4. Computer___Network/20230516_00.html
  5. Computer___FreeBSD/20220621_0.html
  6. Computer___Python/20220715_0.html
  7. Computer___Network/20230508_00.html
  8. Food___Taiwan/20220429_0.html
  9. Computer___Network/20240130_00.html
  10. Computer___NetBSD/20220817_3.html
  11. Computer___Python/20220410_0.html
  12. Computer___Network/20240416_00.html
  13. Computer___NetBSD/20230119_00.html
  14. Computer___Debian/20210223_1.html
  15. Computer___Python/20221013_0.html
  16. Computer___Python/20210124_0.html
  17. Computer___NetBSD/20220428_0.html
  18. Computer___NetBSD/20220818_1.html
  19. Computer___NetBSD/20240101_02.html
  20. Science___Math/20220420_0.html
  21. Computer___Python/20240101_00.html
  22. Computer___NetBSD/20220808_0.html
  23. Computer___TeX/20230503_00.html
  24. Science___Astronomy/20220503_0.html
  25. Computer___NetBSD/20230515_00.html
  26. Computer___Network/20220413_1.html
  27. Computer___NetBSD/20210127_0.html
  28. Computer___TeX/20231107_00.html
  29. Computer___Python/20220816_1.html
  30. Computer___Python/20230717_01.html


HTML file generated by Kinoshita Daisuke.