简体   繁体   中英

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

I have two list: this:

list1(has way more items)

['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)

['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?

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.

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. It will simply do nothing on Unix enviorments.

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. This is what I tried in Python:

  • given list1, list2

  • we want to grab files from list1 that match the name as those 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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