# -*- coding: cp949 -*-
from functools import *
def intersect(*ar):
"교집합"
return reduce(__intersectSC, ar)
def __intersectSC(listX, listY):
setList = []
for x in listX:
if x in listY:
setList.append(x)
return setList
def difference(*ar):
"차집합"
setList = []
intersectSet = intersect(*ar)
unionSet = union(*ar)
for x in unionSet:
if not x in intersectSet:
setList.append(x)
return setList
def union(*ar):
"합집합"
setList = []
for item in ar:
for x in item:
if not x in setList:
setList.append(x)
return setList
simpleset.py로 저장
맥은 /Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2에 저장
>>> import simpleset
>>> dir(simpleset)
['WRAPPER_ASSIGNMENTS', 'WRAPPER_UPDATES', '__builtins__', '__cached__', '__doc__', '__file__', '__intersectSC', '__name__', '__package__', 'cmp_to_key', 'difference', 'intersect', 'lru_cache', 'partial', 'reduce', 'total_ordering', 'union', 'update_wrapper', 'wraps']
>>> setA = [1, 3, 7, 10]
>>> setB = [2, 3, 4, 9]
>>> simpleset.union(setA, setB)
[1, 3, 7, 10, 2, 4, 9]
>>> simpleset.intersect(setA, setB)