-
[Python tutorial] 24. Set Methods코딩/Python 2022. 12. 21. 17:29728x90
set.add(elmnt)
Adds an element to the set
fruits = {"apple", "banana", "cherry"} fruits.add("orange")
set.clear()
Removes all the elements from the set
fruits = {"apple", "banana", "cherry"} fruits.clear()
set.copy()
Returns a copy of the set
fruits = {"apple", "banana", "cherry"} x = fruits.copy()
set.difference(set)
Returns a set containing the difference between two or more sets
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} z = x.difference(y)
set.difference_update(set)
Removes the items in this set that are also included in another, specified set
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} x.difference_update(y)
set.discard(value)
Remove the specified item
fruits = {"apple", "banana", "cherry"} fruits.discard("banana")
set.intersection(set1, set2 ... etc)
Returns a set, that is the intersection of two or more sets
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} z = x.intersection(y)
set.intersection_update(set1, set2 ... etc)
Removes the items in this set that are not present in other, specified set(s)
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} x.intersection_update(y)
set.isdisjoint(set)
Returns whether two sets have a intersection or not
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "facebook"} z = x.isdisjoint(y)
set.issubset(set)
Returns whether another set contains this set or not
x = {"a", "b", "c"} y = {"f", "e", "d", "c", "b", "a"} z = x.issubset(y)
set.issuperset(set)
Returns whether this set contains another set or not
x = {"f", "e", "d", "c", "b", "a"} y = {"a", "b", "c"} z = x.issuperset(y)
set.pop()
Removes an element from the set
fruits = {"apple", "banana", "cherry"} fruits.pop()
set.remove(item)
Removes the specified element
fruits = {"apple", "banana", "cherry"} fruits.remove("banana")
set.symmetric_difference(set)
Returns a set with the symmetric differences of two sets
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} z = x.symmetric_difference(y)
set.symmetric_difference_update(set)
inserts the symmetric differences from this set and another
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} x.symmetric_difference_update(y)
set.union(set1, set2...)
Return a set containing the union of sets
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} z = x.union(y)
set.update(set)
Update the set with another set, or any other iterable
x = {"apple", "banana", "cherry"} y = {"google", "microsoft", "apple"} x.update(y)
728x90'코딩 > Python' 카테고리의 다른 글
[Python tutorial] 26. Keyword (0) 2022.12.21 [Python tutorial] 25. File Methods (0) 2022.12.21 [Python tutorial] 23. Tuple Methods (0) 2022.12.21 [Python tutorial] 22. Dictionary methods (0) 2022.12.21 [Python tutorial] 21. List/Array methods (0) 2022.12.21