是的,可以使用
collections.defaultdict:
>>> from collections import defaultdict>>> dic = defaultdict(list)>>> lis = [['test_data', 'new_directory', 'ok.txt'], ['test_data', 'reads_1.fq'], for item in lis: key = "/".join(item[:-1]) dic[key].append(item[-1])... >>> dicdefaultdict(<type 'list'>,{'test_data': ['reads_1.fq', 'test_ref.fa'], 'test_data/new_directory': ['ok.txt']})
使用简单
dict:
>>> dic = {}>>> for item in lis: key = "/".join(item[:-1]) dic.setdefault(key, []).append(item[-1])... >>> dic{'test_data': ['reads_1.fq', 'test_ref.fa'], 'test_data/new_directory': ['ok.txt']}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)