티스토리 뷰
set 메서드
📎 메서드 -> 타입은 반환 타입을 적어 놓은 것입니다.
✅ set.add(elmnt) -> None
set에 요소를 추가한다.
만약 요소가 존재할 경우 요소는 추가되지 않는다.
Parameter Values
elmnt : Required. The element to add to the set
Example
thisset = {"apple", "banana", "cherry"}
thisset.add("apple")
print(thisset) # {'banana', 'apple', 'cherry'}
✅ set.pop() -> type of elem
set에서 랜덤으로 요소 하나를 제거한 후 반환한다.
Parameter Values
No parameter values.
Example
fruits = {"apple", "banana", "cherry"}
x = fruits.pop()
print(x) # cherry
✅ set.remove(item) -> None
set에서 특정 요소롤 제거한다. 만약 요소가 존재하지 않을 경우 error발생
📎 discard()와 기능은 동일하지만 요소가 존재하지 않으면 error가 발생한다.
Parameter Values
item : Required. The item to search for, and remove
Example
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits) # {'apple', 'cherry'}
✅ set.discard(value) -> None
set에서 특정 요소를 제거한다. 만약 요소가 존재하지 않아도 error가 발생하지 않는다.
📎 remove()와 기능은 동일하지만 요소가 존재하지 않아도 error가 발생하지 않는다.
Parameter Values
value : Required. The item to search for, and remove
Example
fruits = {"apple", "banana", "cherry"}
fruits.discard("banana")
print(fruits) # {'cherry', 'apple'}
✅ set.clear() -> None
set의 모든 요소를 제거한다.
Parameter Values
No Parameters
Example
fruits = {"apple", "banana", "cherry"}
fruits.clear()
print(fruits) # set()
✅ set.copy()
set의 복사본을 반환한다.
Parameter Values
No parameters
Example
fruits = {"apple", "banana", "cherry"}
x = fruits.copy()
print(x) # {'banana', 'apple', 'cherry'}
✅ set.update(set) -> None
다른 set또는 다른 iterable의 요소들을 추가함으로써 현재 set을 업데이트한다.
📎 만약 두 set에 요소가 존재할 경우 업데이트된 set에는 오직 한 개만 존재한다.
Parameter Values
set : Required. The iterable insert into the current set
Example
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
x.update(y)
print(x) # {'microsoft', 'apple', 'google', 'banana', 'cherry'}
✅ set.union(set1, set2...) -> set
여러 set의 합집합을 의미한다.
Parameter Values
set1 : Required. The iterable to unify with
set2 : Optional. The other iterable to unify with. You can compare as many iterables as you like. (Separate each iterable with a comma)
Example
x = {"a", "b", "c"}
y = {"f", "d", "a"}
z = {"c", "d", "e"}
result = x.union(y, z) # result = x | y | z와 같다.
print(result) # {'c', 'd', 'e', 'a', 'f', 'b'}
✅ set.intersection(set1, set2 ... etc) -> set
여러 set의 교집합을 의미한다.
Parameter Values
set1 : Required. The set to search for equal items in
set2 : Optional. The other set to search for equal items in. You can compare as many sets you like. (Separate the sets with a comma)
Example
x = {"a", "b", "c"}
y = {"c", "d", "e"}
z = {"f", "g", "c"}
result = x.intersection(y, z) # result = x & y & z와 같다.
print(result) # {'c'}
✅ set.difference(set) -> set
두 번째 set에 존재하지 않고 첫 번째 set에만 존재하는 요소들을 반환한다. (차집합을 의미한다.)
Parameter Values
set : Required. The set to check for differences in
Example
x = {"apple", "banana", "cherry"}
y = {"google", "microsoft", "apple"}
z = x.difference(y) # z = x - y와 같다.
print(z) # {'banana', 'cherry'}
z = y.difference(x) # z = y - x와 같다.
print(z) # {'microsoft', 'google'}
References
'language > Python' 카테고리의 다른 글
| [Python] 파이썬에서 asterisk(*)가 갖는 여러 기능 (1) | 2022.09.21 |
|---|---|
| [Python] Tuple(튜플) 메서드 정리 (0) | 2022.09.19 |
| [Python] Dictionary(딕셔너리) 메서드 정리 (0) | 2022.09.18 |
| [Python] List(리스트) 메서드 정리 (0) | 2022.09.18 |
| [Python] 외워두면 좋은 String(문자열) 메서드 (0) | 2022.09.17 |
- Total
- Today
- Yesterday
- set메서드
- 백준 15684
- 백준 14891
- 리스트 메서드
- Python
- 백준 15685
- sorted
- 백준 16234
- Split
- 백준 사다리 조작
- 백준 15686
- 백준 14890
- 백준 경사로
- setmethod
- list method
- 백준
- 백준 톱니바퀴
- 백준 인구 이동
- 백준 드래곤 커브
- tuple method
- JOIN
- 최대 공약수
- 최소 공배수
- 파이썬
- **kwargs
- 백준 15683
- 백준 치킨 배달
- 백준 감시
- Unpacking
- *args
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 |