python之列表集合加减与或的计算

## set_calc.py

import sys
import os
import re

sList = []
dList = []

def getLinesOfFile(fname):
    fp = open(fname,"r")
    cnt = fp.read()
    return re.split(r'\s',cnt)



def SubList(aList,bList):# in a ,not in b
    tmp = []
    for a in aList:
        if a not in bList:
            tmp.append(a)
    return tmp

def AndList(aList,bList):# in a and in b
    tmp = []
    for a in aList:
        if a in bList:
            tmp.append(a)
    return tmp

def OrList(aList,bList):# in a or in b
    tmp = OnceForList(aList)
    bList = OnceForList(bList)
    for a in bList:
        if a not in tmp:
            tmp.append(a)
    return tmp
    
def OnceForList(aList):# ele not repeat in list
    tmp = []
    for a in aList:
        if a not in tmp:
            tmp.append(a)
    return tmp


def showList(list):
    print "len = ",len(list)
    print "=============================="
    for it in list:
        print "item = ",it



sList = getLinesOfFile(sys.argv[1])
dList = getLinesOfFile(sys.argv[2])

showList(SubList(dList,sList))
showList(AndList(dList,sList))

@@用法:

src.txt AA BB CC

des.txt  CC DD EE

calc.py src.txt des.txt

你可能感兴趣的:(Python)