当前位置:网站首页>Set implements list search and exclusion

Set implements list search and exclusion

2022-06-13 08:05:00 sichuanwww

Use geometry set Arithmetic , Realize list search and exclusion

set Difference value   s1-s2

set intersection s1&s2

set Combine s1|s2

import os
import shutil

# Read student list 
listName=[]
with open("name.txt",encoding="utf-8") as file:
    for item in file:
        item = item.rstrip('\n')
        listName.append(item)
#print(listName)

# Read all the files in the directory 
file_dir=" The application form "
for root, dirs, files in os.walk(file_dir):
    pass
#print(files) 
   
# Process the list of people who have papers     
full_path ='nameWithPapaer.txt'
paperfile = open(full_path, 'w')
listPaper=[]
# Match file 
target_path=" Destination folder "
for stuName in listName:
    for stuPaper in files:
        if stuPaper.count(stuName)>0:

            # Copy files 
            src_file=" The application form \\"+stuPaper
            shutil.copy(src_file, target_path)            
            print(stuPaper," Copy complete ") 
            
            # Writing documents 
            paperfile.write(stuName+"\n")
            listPaper.append(stuName) 

paperfile.close()   

# List of persons handling papers without papers 
full_path ='nameNoPapaer.txt'
nopaperfile = open(full_path, 'w')

# utilize set Perform the difference operation 
s1=set(listName) 
s2=set(listPaper)
s3=set(s1-s2)

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

print(" Finished processing !")
input(" Any key ends ")



  

Set operations

原网站

版权声明
本文为[sichuanwww]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130802077905.html