简体   繁体   English

压缩两个具有相同文件名的列表中的项目?

[英]Zip items from two lists that have the same file name?

I have two list: this:我有两个清单:这个:

list1(has way more items) list1(有更多项目)

['C:\\Users\\user\\Desktop\\prog1\\merge\\AST\\AST.shp',
 'C:\\Users\\user\\Desktop\\prog1\\merge\\ASTI\\ASTI.shp',
 'C:\\Users\\user\\Desktop\\prog1\\merge\\ASTO\\ASTO.shp']

and this:还有这个:

list2(has way more items) list2(有更多项目)

['C:\\Users\\user\\Desktop\\programs\\merge\\AST\\AST.shp',
 'C:\\Users\\user\\Desktop\\programs\\merge\\ASTI\\ASTI.shp',
 'C:\\Users\\user\\Desktop\\programs\\merge\\AWE\\AWE.shp',  #THIS IS EXTRA
 'C:\\Users\\user\\Desktop\\programs\\merge\\ASTO\\ASTO.shp']

How to ensure that the pairs will match with the corresponding same name on the other list after the zip?如何确保这些对将与 zip 后另一个列表中的相应同名匹配?

Maybe we match with their previous folder?也许我们与他们以前的文件夹匹配? Like:喜欢:

if list1[0].split('\\')[-2] == list2[0].split('\\')[-2]:
      final = [(f,s) for f,s in zip(list1,list2)]
      final

wanted final output :想要最终输出:

[('C:\\Users\\user\\Desktop\\prog1\\merge\\AST\\AST.shp',
  'C:\\Users\\user\\Desktop\\programs\\merge\\AST\\AST.shp'),etc..]

I would just group the files with a collections.defaultdict() , then output the pairs of length 2 in a separate list.我只是用collections.defaultdict()对文件进行分组,然后在单独的列表中输出长度为 2 的对。

Demo:演示:

from os.path import basename
from collections import defaultdict
from pprint import pprint

f1 = [
    "C:\\Users\\user\\Desktop\\prog1\\merge\\AST\\AST.shp",
    "C:\\Users\\user\\Desktop\\prog1\\merge\\ASTI\\ASTI.shp",
    "C:\\Users\\user\\Desktop\\prog1\\merge\\ASTO\\ASTO.shp",
]

f2 = [
    "C:\\Users\\user\\Desktop\\programs\\merge\\AST\\AST.shp",
    "C:\\Users\\user\\Desktop\\programs\\merge\\ASTI\\ASTI.shp",
    "C:\\Users\\user\\Desktop\\programs\\merge\\AWE\\AWE.shp",  # THIS IS EXTRA
    "C:\\Users\\user\\Desktop\\programs\\merge\\ASTO\\ASTO.shp",
]

files = defaultdict(list)
for path in f1 + f2:
    filename = path.split('\\')[-1]
    files[filename].append(path)

pairs = [tuple(v) for k, v in files.items() if len(v) == 2]
pprint(pairs)

Output:输出:

[('C:\\Users\\user\\Desktop\\prog1\\merge\\AST\\AST.shp',
  'C:\\Users\\user\\Desktop\\programs\\merge\\AST\\AST.shp'),
 ('C:\\Users\\user\\Desktop\\prog1\\merge\\ASTI\\ASTI.shp',
  'C:\\Users\\user\\Desktop\\programs\\merge\\ASTI\\ASTI.shp'),
 ('C:\\Users\\user\\Desktop\\prog1\\merge\\ASTO\\ASTO.shp',
  'C:\\Users\\user\\Desktop\\programs\\merge\\ASTO\\ASTO.shp')]

Note: Usingos.path.basename() to extract the filename from Windows paths will only work on Windows.注意:使用os.path.basename()从 Windows 路径中提取文件名仅适用于 Windows。 It will simply do nothing on Unix enviorments.它在 Unix 环境中根本不会做任何事情。

This does not address the question directly using zip(), but in case if someone landed here looking for a way to grab files from two lists that have the same file name.这并不能直接使用 zip() 解决问题,但如果有人来到这里寻找一种从两个具有相同文件名的列表中获取文件的方法。 This is what I tried in Python:这是我在 Python 中尝试过的:

  • given list1, list2给定list1,list2

  • we want to grab files from list1 that match the name as those in list2我们想从 list1 中获取与 list2 中名称匹配的文件

    output_list = [list1 for list1 in list2] output_list = [list1 for list1 in list2]

And you can continue and zip(list1, list2) to ensure filenames in two lists are the same.您可以继续 zip(list1, list2) 以确保两个列表中的文件名相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM