set()
函數在 Python 數據清洗中的應用主要是用于去除重復元素和實現集合運算
my_list = [1, 2, 3, 4, 4, 5, 6, 6]
unique_list = list(set(my_list))
print(unique_list) # 輸出: [1, 2, 3, 4, 5, 6]
setA = {1, 2, 3, 4}
setB = {3, 4, 5, 6}
intersection = setA.intersection(setB)
print(intersection) # 輸出: {3, 4}
setA = {1, 2, 3, 4}
setB = {3, 4, 5, 6}
union = setA.union(setB)
print(union) # 輸出: {1, 2, 3, 4, 5, 6}
setA = {1, 2, 3, 4}
setB = {3, 4, 5, 6}
difference = setA.difference(setB)
print(difference) # 輸出: {1, 2}
setA = {1, 2, 3, 4}
setB = {3, 4, 5, 6}
symmetric_difference = setA.symmetric_difference(setB)
print(symmetric_difference) # 輸出: {1, 2, 5, 6}
通過使用 set()
函數和集合運算,你可以更有效地處理和清洗數據。