Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.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-将两个for循环合并为一个_Python - Fatal编程技术网

Python-将两个for循环合并为一个

Python-将两个for循环合并为一个,python,Python,我有一个工作功能,我想提高它的效率。 我想将最后两个for循环缩短为一个循环,但我不知道如何缩短。 我考虑的唯一方法是在两个不同的位置同时运行两个Valiable。 可能吗?它的正确语法是什么 以下是原始代码: def mvPPMs(outDir): # rename all ppms and move them to the dump folder for dir in os.listdir(outDir): if os.path.isdir(os.path.j

我有一个工作功能,我想提高它的效率。 我想将最后两个for循环缩短为一个循环,但我不知道如何缩短。 我考虑的唯一方法是在两个不同的位置同时运行两个Valiable。 可能吗?它的正确语法是什么

以下是原始代码:

def mvPPMs(outDir):
    # rename all ppms and move them to the dump folder
    for dir in os.listdir(outDir):
        if os.path.isdir(os.path.join(outDir, dir)):
            for camDir in os.listdir(os.path.join(outDir, dir, 'dump')):
                if os.path.isdir(os.path.join(outDir, dir, 'dump', camDir)):
                    for file in os.listdir(os.path.join(outDir, dir, 'dump', camDir)):
                        if 'ppm' in file:
                            shutil.move((os.path.join(outDir, dir, 'dump', camDir, file)), (
                                os.path.join(outDir, dir, 'dump', '%s%s_L-2.ppm' % (os.path.basename(camDir), file.split('t0_')[-1][0]))))
                                             
                    for file in os.listdir(os.path.join(outDir, dir, 'dump', camDir, 'dump')):
                        if 'ppm' in file:
                            shutil.move((os.path.join(outDir, dir, 'dump', camDir, 'dump', file)), (os.path.join(outDir, dir, 'dump', '%s%s_L-2.ppm' % (os.path.basename(camDir), file.split('t0_')[-1][0]))))

                    shutil.rmtree(os.path.join(outDir, dir, 'dump', camDir))
这就是我要做的,请用正确的语法重写它:

                    for file1, file2 in [os.listdir(os.path.join(outDir, dir, 'dump', camDir)), os.listdir(os.path.join(outDir, dir, 'dump', camDir, 'dump'))]:
                        if 'ppm' in file1:
                            shutil.move((os.path.join(outDir, dir, 'dump', camDir, file)), (
                                os.path.join(outDir, dir, 'dump',
                                             '%s%s_L-2.ppm' % (os.path.basename(camDir), file.split('t0_')[-1][0]))))
                        if 'ppm' in file2:
                            shutil.move((os.path.join(outDir, dir, 'dump', camDir, 'dump', file)), (
                                os.path.join(outDir, dir, 'dump', '%s%s_L-2.ppm' % (os.path.basename(camDir), file.split('t0_')[-1][0]))))

                    shutil.rmtree(os.path.join(outDir, dir, 'dump', camDir))
我希望file1在第一个路径中搜索,file2在第二个路径中搜索,然后检查每个变量。 请告诉我这是可能的

谢谢大家

我建议你看看