Python 的 set 函數可以與其他函數結合使用,以實現更復雜的功能
my_set = {1, 2, 3, 4, 5}
for element in my_set:
print(element)
my_set = {1, 2, 3, 4, 5}
new_set = {x * 2 for x in my_set}
print(new_set)
my_set = {1, 2, 3, 4, 5}
length = len(my_set)
print(length)
my_set = {1, 2, 3}
my_set.add(4)
print(my_set)
set1 = {1, 2, 3}
set2 = {3, 4, 5}
set1.update(set2)
print(set1)
my_set = {1, 2, 3, 4, 5}
my_set.remove(3)
print(my_set)
my_set = {1, 2, 3, 4, 5}
my_set.discard(6)
print(my_set)
my_set = {1, 2, 3, 4, 5}
my_set.clear()
print(my_set)
my_set = {5, 3, 1, 4, 2}
sorted_set = sorted(my_set)
print(sorted_set)
這些只是結合其他函數使用 Python set 函數的一些例子。你可以根據需要選擇合適的函數來實現所需的功能。