关闭

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

2598人阅读 评论(0) 收藏 举报
分类:

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

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:144529次
    • 积分:3992
    • 等级:
    • 排名:第8151名
    • 原创:243篇
    • 转载:159篇
    • 译文:0篇
    • 评论:2条
    文章分类
    最新评论