Home > Back-end >  Sublist filenames based on parent folder and partially similar names
Sublist filenames based on parent folder and partially similar names

Time:12-11

I have the following list:

mesh_files = ['../data/Tombstone1.obj', '../data/Tombstone1_p1.obj', '../data/Tombstone1_p2.obj', '../data/Tombstone2.obj', '../data/box_8/frag_1__final.ply', '../data/box_8/frag_1_final.ply', '../data/box_8/frag_2__final.ply', '../data/box_8/frag_2_final.ply', '../data/box_8/frag_3__final.ply', '../data/box_8/frag_3_final.ply', '../data/frag_1__final.ply', '../data/frag_1_final.ply', '../data/parasaurolophus_6700.ply']

print('\n'.join(mesh_files))
../data/Tombstone1.obj
../data/Tombstone1_p1.obj
../data/Tombstone1_p2.obj
../data/Tombstone2.obj
../data/box_8/frag_1__final.ply
../data/box_8/frag_1_final.ply
../data/box_8/frag_2__final.ply
../data/box_8/frag_2_final.ply
../data/box_8/frag_3__final.ply
../data/box_8/frag_3_final.ply
../data/frag_1__final.ply
../data/frag_1_final.ply
../data/parasaurolophus_6700.ply

Is there a fast/direct way to split the above list to the following sublists:

[['../data/Tombstone1.obj', '../data/Tombstone1_p1.obj', '../data/Tombstone1_p2.obj'], 
['../data/Tombstone2.obj'], 
['../data/box_8/frag_1__final.ply', '../data/box_8/frag_1_final.ply'], 
['../data/box_8/frag_2__final.ply', '../data/box_8/frag_2_final.ply'], 
['../data/box_8/frag_3__final.ply', '../data/box_8/frag_3_final.ply'], 
['../data/frag_1__final.ply', '../data/frag_1_final.ply'], 
['../data/parasaurolophus_6700.ply']]

As I see it, I would consider as conditions the parent folder and part of the filenames. However, I would like to avoid for loops.

Thanks.

CodePudding user response:

You can use recursion:

import re, collections
def group(d, p=[]):
   c = collections.defaultdict(list)
   for a, *b in d:
      c[a].append(b)
   for a, b in c.items():
      if any(len(i) == 1 for i in b):
         v = [x for y in b for x in ([p [a] y] if len(y)==1 else group([y],p [a]))]
         yield [j for k in v for j in ([k] if all(isinstance(i, str) for i in k) else k)]
      else:
         yield from group(b, p [a])

mesh_files = ['../data/Tombstone1.obj', '../data/Tombstone1_p1.obj', '../data/Tombstone1_p2.obj', '../data/Tombstone2.obj', '../data/box_8/frag_1__final.ply', '../data/box_8/frag_1_final.ply', '../data/box_8/frag_2__final.ply', '../data/box_8/frag_2_final.ply', '../data/box_8/frag_3__final.ply', '../data/box_8/frag_3_final.ply', '../data/frag_1__final.ply', '../data/frag_1_final.ply', '../data/parasaurolophus_6700.ply']
r = list(group([re.findall('[a-zA-Z0-9] ', i) for i in mesh_files]))
n = iter(mesh_files)
result = [[next(n) for _ in k] for k in r]

Output:

[['../data/Tombstone1.obj', '../data/Tombstone1_p1.obj', '../data/Tombstone1_p2.obj'], 
 ['../data/Tombstone2.obj'], 
 ['../data/box_8/frag_1__final.ply', '../data/box_8/frag_1_final.ply'], 
 ['../data/box_8/frag_2__final.ply', '../data/box_8/frag_2_final.ply'], 
 ['../data/box_8/frag_3__final.ply', '../data/box_8/frag_3_final.ply'], 
 ['../data/frag_1__final.ply', '../data/frag_1_final.ply'], 
 ['../data/parasaurolophus_6700.ply']]
  • Related