我基本上试图将文件路径字符串分隔到同一路径中的文件列表中。假设我有这个清单:
files = ['user/hey.jpg','user/folder1/1.txt','user/folder1/folder2/random.txt',
'user/folder1/blah.txt','user/folder3/folder4/folder5/1.txt',
'user/folder3/folder4/folder5/3.txt','user/folder3/folder4/folder5/2.txt',
'user/1.jpg']
我希望获得此输出(不关心订单):
[
['user/folder1/1.txt','user/folder1/blah.txt']
['user/folder1/folder2/random.txt']
['user/folder3/folder4/folder5/1.txt',
'user/folder3/folder4/folder5/2.txt','user/folder3/folder4/folder5/3.txt']
['user/1.jpg','user/hey.jpg']
]
很抱歉,如果我的例子很草率。在手机上打字并不好玩。任何帮助表示赞赏!
答案 0 :(得分:7)
使用os.path.dirname
作为sorted
中的密钥,groupby
函数会引导您获得结果:
import os
from itertools import groupby
[list(g) for _, g in groupby(sorted(files, key=os.path.dirname), key=os.path.dirname)]
#[['user/hey.jpg', 'user/1.jpg'],
# ['user/folder1/1.txt', 'user/folder1/blah.txt'],
# ['user/folder1/folder2/random.txt'],
# ['user/folder3/folder4/folder5/1.txt',
# 'user/folder3/folder4/folder5/3.txt',
# 'user/folder3/folder4/folder5/2.txt']]
答案 1 :(得分:2)
试试这个:
import collections
results = defaultdict(list)
for path in files:
results[os.path.dirname(path)].append(path)
答案 2 :(得分:1)
另一个选择,获取一组唯一目录,然后使用嵌套列表理解。
from os.path import dirname
unique_dirs = set(dirname(s) for s in files)
output = [[f for f in files if dirname(f) == directory]
for directory in unique_dirs]
答案 3 :(得分:1)
你可以这样做
>>> sorted_files = [ x for x in sorted(files, key=lambda x: x.count("/"))]
>>> max_dash = max([x.count("/") for x in sorted_files])
>>> max_dash
4
>>> newlist = [[y for y in sorted_files if y.count("/")==x] for x in range(max_dash)]
>>> newlist
[[], ['user/hey.jpg', 'user/1.jpg'], ['user/folder1/1.txt', 'user/folder1/blah.txt'], ['user/folder1/folder2/random.txt']]