본문 바로가기

컴퓨터/Python

모듈 만들기

# -*- 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)

'컴퓨터 > Python' 카테고리의 다른 글

모듈 임포트 파헤치기  (0) 2013.07.22
모듈 임포트  (0) 2013.07.22
다중상속  (0) 2013.07.18
상속  (0) 2013.07.18
연산자 중복 정의  (0) 2013.07.17