1. 程式人生 > 實用技巧 >計算機軟考認證考試難嗎?如何通過?-弘博創新

計算機軟考認證考試難嗎?如何通過?-弘博創新

  • 內建方法
set.add(elmnt),elmnt為要新增的元素
無返回值
fruits = {"apple", "banana", "cherry"}
fruits.add("orange") 
print(fruits)
# {'apple', 'banana', 'orange', 'cherry'}
set.clear()

fruits = {"apple", "banana", "cherry"}
fruits.clear()
print(fruits)
# set()
set.copy()
fruits = {"apple", "banana", "cherry"}
x = fruits.copy()
print(x)
# {'cherry', 'banana', 'apple'}
set.difference(set),set為用於計算差集的集合
返回一個計算完成的集合
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.difference(y) 
print(z)
# {'cherry', 'banana'}
set.difference_update(set),set為用於計算差集的集合
無,其會直接在原集合上進行修改
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.difference_update(y) 
print(x)
# {'cherry', 'banana'}
set.discard(value),value為要移除的元素

fruits = {"apple", "banana", "cherry"}
fruits.discard("banana") 
print(fruits)
# {'cherry', 'apple'}
set.intersection(set1, set2 ... etc),set1,set2為需要查詢相同元素的組合
返回一個新的集合
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
z = x.intersection(y) 
print(z)
# {'apple'}
set.intersection_update(set1, set2 ... etc),set1,set2為需要查詢相同元素的組合
無返回值,會直接將原集合變為其交集元素的集合
x = {"apple", "banana", "cherry"}  # y 集合不包含 banana 和 cherry,被移除 
y = {"google", "runoob", "apple"}
x.intersection_update(y) 
print(x)
# {'apple'}
set.isdisjoint(set),set為要比較的集合
返回布林值,如果不包含返回 True,否則返回 False
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "facebook"}
z = x.isdisjoint(y) 
print(z)
# True
set.issubset(set),set為要比較查詢的集合
返回布林值,如果都包含返回 True,否則返回 False
x = {"a", "b", "c"}
y = {"f", "e", "d", "c", "b", "a"}
z = x.issubset(y) 
print(z)
# True
set.issuperset(set),set為要比較查詢的集合
返回布林值,如果都包含返回 True,否則返回 False
x = {"f", "e", "d", "c", "b", "a"}
y = {"a", "b", "c"}
z = x.issuperset(y) 
print(z)
# True
set.pop()
返回要移除的元素
fruits = {"apple", "banana", "cherry"}
fruits.pop() 
print(fruits)
# {'apple', 'banana'}
set.remove(item),item為要移除的元素
無返回值
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana") 
print(fruits)
# {'cherry', 'apple'}
set.symmetric_difference(set),set為集合
返回一個新的集合,其取並集並移除兩個元素中為交集的元素
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
z = x.symmetric_difference(y) 
print(z)
# {'google', 'cherry', 'banana', 'runoob'}
set.symmetric_difference_update(set),set為要檢測的集合
無,其會直接在原集合上修改為並集去除交集的元素
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
x.symmetric_difference_update(y) 
print(x)
# {'google', 'cherry', 'banana', 'runoob'}
set.union(set1, set2...),set1,set2為要進行合併的集合
返回一個合併的新集合
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
z = x.union(y) 
print(z)
# {'cherry', 'runoob', 'google', 'banana', 'apple'}
set.update(set),set為元素的集合
無返回值,其會新增元素
x = {"apple", "banana", "cherry"}
y = {"google", "runoob", "apple"}
x.update(y) 
print(x)
# {'banana', 'apple', 'google', 'runoob', 'cherry'}