gpt4 book ai didi

python - 如何对相关的 .tif 文件进行分组?

转载 作者:太空宇宙 更新时间:2023-11-03 17:42:05 25 4
gpt4 key购买 nike

我正在尝试对与 .tif 图像关联的相关文件进行分组。从列表中可以看到,每组有 7 个相关文件。我正在寻找一种对这些文件进行分组的方法,以便我可以通过 shutil.move() 将它们移动到各个文件夹中。

以下脚本是我根据 this answer 尝试失败的脚本。结果与输入 files 列表相同。

我如何调整它来执行我想要的分组?

import os, itertools

files = ['F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tfw',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.aux.xml',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.ovr',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.vat.cpg',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.vat.dbf',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.xml',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tfw',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.aux.xml',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.ovr',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.vat.cpg',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.vat.dbf',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.xml']

test = sorted(files)
grouped = [list(g) for _, g in itertools.groupby(test, lambda x: x.split('_')[1])]
<小时/>

预期输出:

[['F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tfw',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.aux.xml',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.ovr',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.vat.cpg',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.vat.dbf',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511759_sw.tif.xml'],
['F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tfw',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.aux.xml',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.ovr',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.vat.cpg',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.vat.dbf',
'F:\\juniper_project\\data\\raster\\deliverables\\OR\\reclass\\reclass_4511760_sw.tif.xml']]

最佳答案

关闭!分割列表上的索引是一次性的,应该是2。

尝试

grouped = [list(g) for _, g in itertools.groupby(test, lambda x: x.split('_')[2])]

关于python - 如何对相关的 .tif 文件进行分组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30383989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com