当前位置:网站首页>set实现名单查找与排除

set实现名单查找与排除

2022-06-13 08:02:00 sichuanwww

利用几何set的运算,实现名单查找与排除

set差值  s1-s2

set交集 s1&s2

set并集s1|s2

import os
import shutil

#读取学生名单
listName=[]
with open("name.txt",encoding="utf-8") as file:
    for item in file:
        item = item.rstrip('\n')
        listName.append(item)
#print(listName)

#读取目录下的全部文件
file_dir="申请表"
for root, dirs, files in os.walk(file_dir):
    pass
#print(files) 
   
#处理拥有论文的人员名单    
full_path ='nameWithPapaer.txt'
paperfile = open(full_path, 'w')
listPaper=[]
#匹配文件
target_path="目标文件夹"
for stuName in listName:
    for stuPaper in files:
        if stuPaper.count(stuName)>0:

            #拷贝文件
            src_file="申请表\\"+stuPaper
            shutil.copy(src_file, target_path)            
            print(stuPaper,"拷贝完毕") 
            
            #写文件
            paperfile.write(stuName+"\n")
            listPaper.append(stuName) 

paperfile.close()   

#处理没有论文的人员名单
full_path ='nameNoPapaer.txt'
nopaperfile = open(full_path, 'w')

#利用set进行差值运算
s1=set(listName) 
s2=set(listPaper)
s3=set(s1-s2)

for i in s3:
    nopaperfile.write(i+"\n")
nopaperfile.close()

print("处理完毕!")
input("任意键结束")



  

集合运算

原网站

版权声明
本文为[sichuanwww]所创,转载请带上原文链接,感谢
https://sichuanwww.blog.csdn.net/article/details/125235870