Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 唯一列表是否可以接受小写和大写条目_Python_List_Unique - Fatal编程技术网

Python 唯一列表是否可以接受小写和大写条目

Python 唯一列表是否可以接受小写和大写条目,python,list,unique,Python,List,Unique,我的脚本记录有关目录和子目录中所有唯一文件类型的信息。在创建文件扩展名的唯一列表的过程中,当前代码认为jpg、jpg和jpg是相同的,因此它只在列表中包含其中一个。如何包含所有三个或更多差异 for root, dirs, files in os.walk(SourceDIR, topdown=False): for fl in files: currentFile=os.path.join(root, fl) ext=fl[fl.rfind('.')+1

我的脚本记录有关目录和子目录中所有唯一文件类型的信息。在创建文件扩展名的唯一列表的过程中,当前代码认为jpg、jpg和jpg是相同的,因此它只在列表中包含其中一个。如何包含所有三个或更多差异

for root, dirs, files in os.walk(SourceDIR, topdown=False):
    for fl in files:
        currentFile=os.path.join(root, fl)
        ext=fl[fl.rfind('.')+1:]
        if ext!='':
            if DirLimiter in currentFile:
                List.append(currentFile)
                directory1=os.path.basename(os.path.normpath(currentFile[:currentFile.rfind(DirLimiter)]))
                directory2=(currentFile[len(SourceDIR):currentFile.rfind('\\'+directory1+DirLimiter)])
                directory=directory2+'\\'+directory1
                if directory not in dirList:
                    dirCount+=1
                    dirList.append(directory)


        if ext not in extList:
          extList.append(ext)
stackexchange上的问题中有完整的脚本:

感谢JennaK的进一步调查,我发现jpg报告中的输入实际上包含jpg和jpg,如下所示

> 44;X:\scratch\project\Input\Foreshore and Jetties Package
> 3\487679 - Jetty\IMG_1630.JPG;3755267
> 45;X:\scratch\project\Input\Foreshore and Jetties Package
> 3\487679 - Jetty\IMG_1633.JPG;2447135
> 1;X:\scratch\project\Input\649701 - Hill
> Close\2263.jpg;405328 2;X:\scratch\project\Input\649701 - Hill Close\2264.jpg;372770

因此,它首先获得了所有JPG文件的详细信息,然后是JPG文件,并将它们放在一个报告中,这实际上比有两个报告更方便。我想我的编程比我想象的要好:-)

不,对于
列表
,操作符中的
检查相等性,字符串只有在使用相同大小写时才彼此相等

您可以在这里使用一个集合,并在其中存储所有
directory.lower()
值。集合对于成员资格测试以及列表来说(要快得多):

directories = set()
extensions = set()

for root, dirs, files in os.walk(SourceDIR, topdown=False):
    # ...
        # no need to use `directory.lower() in directories`, just update the set:
        directories.add(directory.lower())

    # ...
    extensions.add(ext.lower())
dirCount
变量稍后很容易导出:

dirCount = len(directories)
您还需要研究更多函数提供的函数,特别是
os.path.splitext()
os.path.relpath()
os.path.join()函数

循环中的文件处理可以简化很多;a:

for fl in files:
    filename = os.path.join(root, fl)
    base, ext = os.path.splitext(filename)
    if ext:
       List.append(filename)
       directory = os.path.relpath(filename, SourceDir)
       directories.add(directory.lower())
       extensions.add(ext)
注意,我在这里只使用了操作系统.path.relpath()
;您的
os.path.basename()
os.path.normpath()
dance加分隔符等都是不必要的复杂

现在,在字里行间的阅读中,似乎你只想把扩展等同于这部分的情况。

在这种情况下,根据
os.path.splitext()
的结果为自己构建一个新文件名:


现在
normalized\u filename
是扩展名降低的文件名,因此您可以根据需要在集合中使用该值。

您真的需要将这些文件添加到列表中吗?我的意思是,也许在“大写”函数后使用字符串检查可以解决您的问题?
ext不在extList中
已经是区分大小写的比较了。如果你的代码不起作用,问题一定在别处。@JanneKarila-你是对的。我只是重新检查了extList,它有所有的case版本。我试图在代码后面跟踪这个问题,并将很快更新我的答案以反映这一点。谢谢,谢谢。我会把这些变化带到船上。根据我对这个问题的编辑,我发现这个问题其实不是问题。我的回答对解决这个问题有帮助吗?您的代码仍然存在不必要的
.normpath()
.basename()
调用的问题,我是否能够解决这些问题?我还没有时间用您的建议改进代码-这可能很糟糕,但仍然有效。谢谢你的意见。
base, ext = os.path.splitext(filename)
normalized_filename = base + ext.lower()