Python

set

Fug 2021. 8. 24. 13:37

Time complexity of Sets

OperationAverage caseWorst Casenotes

x in s O(1) O(n)  
Union s|t O(len(s)+len(t))    
Intersection s&t O(min(len(s), len(t)) O(len(s) * len(t)) replace “min” with “max” if t is not a set
Multiple intersection s1&s2&..&sn   (n-1)*O(l) where l is max(len(s1),..,len(sn))  
Difference s-t O(len(s))  

출처 https://www.geeksforgeeks.org/sets-in-python/

 

Sets in Python - GeeksforGeeks

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

www.geeksforgeeks.org

set에 대해서 살펴보면 일단 한국어로는 "집합"이다

 

되는 건 in not in 처럼 for문에서 들어갈 만한 것에 사용될 수 있고

 

set() 으로 리스트가 문자로 이루어져있다면 그 리스트를 하나하나를 분해해서 집합으로 합칠 수 있다.

 

>>> a='1,2,3,4,5'
>>> b=set(a)
>>> b
{',', '2', '3', '1', '4', '5'}

 

또한 여기서 알 수있는것 4가지 합치기 공통점 찾기 차이점 찾기 비우기

 

문자로는 각각 (| , .add()) , (&, .intersection()) , (-, .difference()) (clear()) )

 

로 나타낼 수있다.