基于父文件夹和部分相似名称的子列表文件名
Sublist filenames based on parent folder and partially similar names
我有以下列表:
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
是否有 fast/direct 方法将上述列表拆分为以下子列表:
[['../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']]
在我看来,我会将父文件夹和部分文件名视为条件。但是,我想避免 for
循环。
谢谢。
你可以使用递归:
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]
输出:
[['../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']]
我有以下列表:
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
是否有 fast/direct 方法将上述列表拆分为以下子列表:
[['../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']]
在我看来,我会将父文件夹和部分文件名视为条件。但是,我想避免 for
循环。
谢谢。
你可以使用递归:
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]
输出:
[['../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']]