Python:创建非匹配值的列表

2024-06-01 13:37:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在研究一个程序,它搜索一个文件夹,然后根据输入列表中的一系列变量找到匹配的文件名,然后将它们复制到一个文件夹中。这个程序可以工作,但是现在我想给它添加一个额外的层;得到一个不匹配的样本列表,然后输出一个CSV文件。这段代码效率不高,但它完成了任务,尽管我知道它可能没有正确地设置来满足我的要求。在

import os, fnmatch, csv, shutil, operator

#Function created to search through a folder location to for using a specific list of keywords
def locate(pattern, root=os.curdir):
matches = []

for path, dirs, files in os.walk(os.path.abspath(root)):
    for filename in fnmatch.filter(files, pattern):
        matches.append(os.path.join(path, filename))

return matches

#output file created to store the pathfiles
outfile="G:\output.csv"
output=csv.writer(open(outfile,'w'), delimiter=',',quoting=csv.QUOTE_NONE)

#Opens the file and stores the values in each row
path="G:\GIS\Parsons Stuff\samples.csv"
pathfile=open(path,'rb')
openfile=csv.reader((pathfile), delimiter = ',')
samplelist=[]
samplelist.extend(openfile)

#for loop used to return the list of tuples
for checklist in zip(*samplelist):
    print checklist

#an empty list used to store the filepaths of sample locations of interest 
files=[]

#for loop to search for sample id's in a folder and copies the filepath
for x in checklist:
    LocatedFiles=locate(x, "G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\")
    print LocatedFiles
    files.append(LocatedFiles)

# flattens the list called files into a managable list
flattenedpath=reduce(operator.add, files)

#filters out files that match the filter .pdf
filteredpath=[]
filteredpath.append(fnmatch.filter(flattenedpath,"*.pdf*"))

#outputs the file path a .csv file called output
output.writerows(files)

pathfile.close()

#location of where files are going to be copied
dst='C:\\TestFolder\\'

#filters out files that match the filer .pdf
filtered=[]
filtered.append(fnmatch.filter(flattenedpath,"*.pdf*"))
filteredpath=reduce(operator.add,filtered)

#the function set() goes through the list of interest to store a list a unique values.  
delete_dup=set(filteredpath)
delete_dup=reduce(operator.add,zip(delete_dup))

#for loop to copy files in the list delete_dup
for x in delete_dup:
    shutil.copy(x,dst)

我的想法是,由于列表“samplelist”和“files”的长度相同:

^{pr2}$

我应该能够从“files”中提取每个空列表的索引值,并将其传递给一个存储索引值的列表,该列表可用于从“samplelist”中提取元素。在

我曾尝试使用以下链接获取想法,但没有成功:

In Python, how can I find the index of the first item in a list that is NOT some value?

Finding matching and nonmatching items in lists

Finding the index of an item given a list containing it in Python

Pythonic way to compare two lists and print out the differences

下面是名为“samplelist”的列表的输出

('*S42TPZ2*', '*S3138*', '*S2415*', '*S2378*', '*S2310*', '*S2299*', '*S1778*', '*S1777*', '*S1776*', '*S1408*', '*S1340*', '*S1327*', '*RW-61*', '*MW-247*', '*MW-229*', '*MW-228*', '*MW-209*', '*MW-208*', '*MW-193*', '*M51TPZ6*', '*M51TP21*', '*H1013*', '*H1001*', '*H0858*', '*H0843*', '*H0834*', '*H0514*', '*H0451*', '*H0450*', '*EY1TP9*', '*EY1TP7*', '*EY1TP6*', '*EY1TP5*', '*EY1TP4*', '*EY1TP2*', '*EY1TP1*')

下面是名为“files”的列表的输出(我不打算列出所有的输出,因为这是不必要的,只是想让您了解一下列表是什么样子的)

[[], [], ['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S2415.pdf'], ['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S2378.pdf'], ['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\MW-247.S2310.pdf', 'G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S2310.MW-247.pdf', 'G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S2310.pdf'], ['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S2299.pdf'], ['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S1778.pdf'], ['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S1777.pdf'], ['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S1776.pdf'], ['G:\\GIS\\Parsons Stuff\\boring logs\\boring logs\\S1408.pdf']


Tags: ofthetoin列表forpdffiles
1条回答
网友
1楼 · 发布于 2024-06-01 13:37:19

我不太确定这是你想要的,但你不能:

index_list = []
for n, item in enumerate(list):
    if len(item) == 0:
        index_list.append(n)

这段代码将在你的列表上迭代,如果列表包含一个空列表,它将返回空列表的索引并将其添加到另一个列表中!在

相关问题 更多 >