如果无法创建没有循环的列表,那么我会尽量减少创建此列表所花费的时间。目标是在10个左右的文件夹中的每个文件夹中创建100个pdf文件的文件名的二维数组。告诉我你的想法:

    foldernames = [
        'Named_folder00/',
        'Named_folder01/',
        'Named_folder02/',
        'Named_folder03/',
        'Named_folder04/',
        'Named_folder05/',
        'Named_folder06/',
        'Named_folder07/',
        'Named_folder08/',
        'Named_folder09/',
        ]

    pages = []
    for b in xrange(len(foldernames)):
        pg_temp = [
            foldernames[b] + 'title1',
            foldernames[b] + 'title2',
            foldernames[b] + 'title3'
            ]
        pg_temp += [ foldernames[b] + '0' + str(j) for j in xrange(1,10) ]
        pg_temp += [ foldernames[b] + str(k) for k in xrange(10,100) ]
        for c in xrange(len(pg_temp)):
            pg_temp[c] += '.pdf'
        pages.append(pg_temp)

最佳答案

files = ['title1.pdf', 'title2.pdf', 'title3.pdf']
files += [ "%.2d.pdf" % j for j in range(1,100)]
pages = [[folder+file for file in files] for folder in foldernames]


注意:我不知道它有多快,没有进行基准测试。

关于python - 谁能告诉我如何在不使用循环的情况下做到这一点?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9856969/

10-10 05:18