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.
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']]