Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Grouping two lists of filenames using string match

I have list of files ‘L1’ as follows:

L1 = ['D://carrot_25kg', 'D://banana_5kg', 'D://apple_50kg']

I have another list of files ‘L2’ as follows:

L2 = ['D://box3_5kg', 'D://box1_25kg', 'D://box2_50kg']

I needed to group files as follows.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

result = [['D://carrot_25kg', 'D://box1_25kg'],
          ['D://banana_5kg', 'D://box3_5kg'],
          ['D://apple_50kg', 'D://box1_50kg']] #the order is not important

It is to match based on "_xxkg" string.
How can I do it guys?

>Solution :

You are grouping by a suffix, let’s use a dictionary:

d = {
    path.split('_')[-1]: [path]
    for path in L1
}

for path in L2:
    suffix = path.split('_')[-1]
    d[suffix] = d.get(suffix, [])
    d[suffix].append(path)

list(d.values())
[['D://carrot_25kg', 'D://box1_25kg'],
 ['D://banana_5kg', 'D://box3_5kg'],
 ['D://apple_50kg', 'D://box2_50kg']]

Or, using a defaultdictionary:

from collections import defaultdict
from itertools import chain


d = defaultdict(list)

for path in chain(L1, L2):
    suffix = path.split('_')[-1]
    d[suffix].append(path)

list(d.values())
[['D://carrot_25kg', 'D://box1_25kg'],
 ['D://banana_5kg', 'D://box3_5kg'],
 ['D://apple_50kg', 'D://box2_50kg']]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading