-
11723. 집합 (set 원소 추가, 삭제)코테 준비/문자열, 내장함수 2022. 12. 27. 18:02
s=set()
원소 추가: s.add()
원소 삭제
s.remove()
s.discard()
discard의 경우 제거하려는 원소가 없어도 오류가 나지 않지만 remove는 에러가 남
import sys m=int(sys.stdin.readline()) s=set() for i in range(m): command=list(sys.stdin.readline().split()) if command[0]=='add': s.add(int(command[1])) elif command[0]=='remove': if int(command[1]) in s: s.remove(int(command[1])) elif command[0]=='check': if int(command[1]) in s: print("1") else: print("0") elif command[0]=='toggle': if int(command[1]) in s: s.remove(int(command[1])) else: s.add(int(command[1])) elif command[0]=='all': s=set([i for i in range(1,21)]) elif command[0]=='empty': s=set()
'코테 준비 > 문자열, 내장함수' 카테고리의 다른 글
[백준] 1541. 잃어버린 괄호 (0) 2023.01.08 17694. 듣보잡 (set 시간 단축) (0) 2022.12.28 최대공약수, 최소공배수 (0) 2022.12.07 최빈값 구하기 (0) 2022.12.07 아스키코드 변환 (0) 2022.12.07