DAY 18
0

## Day18 Python 基礎 - 集合的使用

• `去重`，把一個列表變成集合，就會自動去重了。
• `關係測試`，測試二組數據之前的交集、差集、聯集等關係。

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

print(list_1, type(list_1))

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 9} <class 'set'>

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_2 = set([0, 2, 6, 66, 22, 8, 4])

print(list_1, list_2)

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_2 = set([0, 2, 6, 66, 22, 8, 4])

print(list_1, list_2)

print(list_1.intersection(list_2))   # 交集

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
{4, 6}

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_2 = set([0, 2, 6, 66, 22, 8, 4])

print(list_1, list_2)

print(list_1.union(list_2))          # 聯集

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
{0, 1, 2, 3, 4, 5, 6, 7, 66, 9, 8, 22}

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_2 = set([0, 2, 6, 66, 22, 8, 4])

print(list_1, list_2)

print(list_1.difference(list_2))     # 差集 in list_1 but not in list_2

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
{1, 3, 5, 9, 7}

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_2 = set([0, 2, 6, 66, 22, 8, 4])

print(list_1, list_2)

print(list_1.difference(list_2))     # 差集 in list_1 but not in list_2
print(list_2.difference(list_1))

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
{1, 3, 5, 9, 7}
{0, 8, 2, 66, 22}

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_2 = set([0, 2, 6, 66, 22, 8, 4])

print(list_1, list_2)

print(list_1.issubset(list_2))       # 子集

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
False

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_2 = set([0, 2, 6, 66, 22, 8, 4])

print(list_1, list_2)

print(list_1.issubset(list_2))       # 子集
print(list_2.issuperset(list_1))     # 父集

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22}
False
False

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_2 = set([0, 2, 6, 66, 22, 8, 4])
list_3 = set([1, 3, 7])

print(list_1, list_2, list_3)

print(list_3.issubset(list_1))       # 子集
print(list_1.issuperset(list_3))     # 父集

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} {1, 3, 7}
True
True

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_2 = set([0, 2, 6, 66, 22, 8, 4])
list_3 = set([1, 3, 7])

print(list_1, list_2, list_3)

print(list_1.symmetric_difference(list_2))

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} {1, 3, 7}
{0, 1, 2, 66, 3, 5, 7, 8, 9, 22}

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_2 = set([0, 2, 6, 66, 22, 8, 4])
list_3 = set([1, 3, 7])
list_4 = set([5, 6, 8])

print(list_1, list_2, list_3)

print(list_3.isdisjoint(list_4))		# Return True if two sets have a null intersection.

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} {1, 3, 7}
True

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_2 = set([0, 2, 6, 66, 22, 8, 4])
list_3 = set([1, 3, 7])
list_4 = set([5, 6, 7, 8])

print(list_1, list_2, list_3)

print(list_3.isdisjoint(list_4))

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} {1, 3, 7}
False

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_2 = set([0, 2, 6, 66, 22, 8, 4])
list_3 = set([1, 3, 7])
list_4 = set([5, 6, 7, 8])

print(list_1, list_2, list_3)

print(list_1 & list_2)          # 交集(intersection)
print(list_1 | list_2)          # 聯集(Union)
print(list_1 - list_2)          # 差集(difference) in list_1 not in list_2
print(list_1 ^ list_2)          # 對稱差集(symmetric_difference)

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 9} {0, 2, 66, 4, 6, 8, 22} {1, 3, 7}
{4, 6}
{0, 1, 2, 3, 4, 5, 6, 7, 66, 9, 8, 22}
{1, 3, 5, 9, 7}
{0, 1, 2, 66, 3, 5, 7, 8, 9, 22}

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

print(list_1)

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 9, 44}

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_1.update([9527, 520, 1314])
print(list_1)

---------------執行結果---------------

{1, 1314, 3, 4, 5, 6, 7, 520, 9, 44, 9527}

Process finished with exit code 0
``````

Method 1: set.remove() 刪除元素，但刪除一個不存在的元素，會噴error

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_1.update([9527, 520, 1314])
list_1.remove(1314)
print(list_1)

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 520, 9, 44, 9527}

Process finished with exit code 0
``````

Method 2: set.pop() 隨機任意刪，並且打印出刪除的元素

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_1.update([9527, 520, 1314])
list_1.remove(1314)
print(list_1)
print(list_1.pop())
print(list_1.pop())
print(list_1.pop())
print(list_1.pop())
print(list_1.pop())
print(list_1.pop())
print(list_1.pop())
print(list_1)

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 520, 9, 44, 9527}
1
3
4
5
6
7
520
{9, 44, 9527}

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_1.update([9527, 520, 1314])
list_1.remove(1314)
print(list_1)

print(list_1)

---------------執行結果---------------

{1, 3, 4, 5, 6, 7, 520, 9, 44, 9527}
{1, 3, 4, 5, 6, 7, 520, 44, 9527}

Process finished with exit code 0
``````

``````#!/usr/bin/env python3
# -*- coding:utf-8 -*-

list_1 = [1, 4, 5, 7, 3, 6, 7, 9]
list_1 = set(list_1)

list_1.update([9527, 520, 1314])
list_1.remove(1314)
print(len(list_1))
print(list_1)

---------------執行結果---------------

10
{1, 3, 4, 5, 6, 7, 520, 9, 44, 9527}

Process finished with exit code 0
``````

Python 學習筆記29