728x90
반응형
728x170
■ List 클래스를 사용해 교집합/차집합/합집합을 구하는 방법을 보여준다.
▶ simpleset.py
from functools import *
def Intersect(*argumentTuple):
"교집합"
intersectionList = reduce(__Intersect, argumentTuple)
return intersectionList
def __Intersect(list1, list2):
targetList = []
for x in list1:
if x in list2:
targetList.append(x)
return targetList
def Difference(*argumentTuple):
"차집합"
targetList = []
intersectionList = Intersect(*argumentTuple)
for x in argumentTuple[0]:
if not x in intersectionList:
targetList.append(x)
return targetList
def Union(*argumentTuple):
"합집합"
targetList = []
for item in argumentTuple:
for x in item:
if not x in targetList:
targetList.append(x)
return sorted(targetList)
▶ main.py
import simpleset
sourceList1 = [1, 3, 7, 10]
sourceList2 = [2, 3, 4, 9 ]
print("소스 리스트 1 : ", sourceList1)
print("소스 리스트 2 : ", sourceList2)
intersectionList = simpleset.Intersect(sourceList1, sourceList2)
print("교집합 : ", intersectionList)
differenceList = simpleset.Difference(sourceList1, sourceList2)
print("차집합(소스 리스트 1 - 소스 리스트 2) : ", differenceList)
unionList = simpleset.Union(sourceList1, sourceList2)
print("합집합 : ", unionList)
728x90
반응형
그리드형(광고전용)
'Python > Common' 카테고리의 다른 글
[PYTHON/COMMON] try ... except ... else ... finally문 사용하기 (0) | 2022.08.21 |
---|---|
[PYTHON/COMMON] 내장 예외 계층 구조 (0) | 2022.08.21 |
[PYTHON/COMMON] 모듈 : __all__ 속성을 사용해 임포트할 하위 패키지 리스트 구하기 (0) | 2022.08.20 |
[PYTHON/COMMON] reload 함수 : 모듈 다시 로드하기 (0) | 2022.08.20 |
[PYTHON/COMMON] import문 : 별칭 사용하기 (0) | 2022.08.19 |
[PYTHON/COMMON] 클래스 : super 함수를 사용해 상위 클래스 메소드 호출하기 (0) | 2022.08.16 |
[PYTHON/COMMON] 클래스 : __mro__ 속성을 사용해 상속 클래스 구하기 (0) | 2022.08.16 |
[PYTHON/COMMON] 클래스 : 다중 상속하기 (0) | 2022.08.16 |
[PYTHON/COMMON] 클래스 : __dict__ 속성을 사용해 클래스 멤버 데이터 구하기 (0) | 2022.08.15 |
[PYTHON/COMMON] 클래스 : 메소드 확장하기 (0) | 2022.08.15 |
댓글을 달아 주세요