杂记
对于列表中套字典的排序:
sorted(list, key=lambda e: e.__getitem__('需要排序的key'), reverse=True) # 降序
将路径转换成字典
def path_to_dict(data):
'''
data :{1: '/home/feysh/Testcases/hashcat/deps/unrar/rs.cpp',
2: '/home/feysh/Testcases/hashcat/src/hashes.c'}
return:{"file_label": "home", "file_id": "home_1", "children": [{"file_label": "feysh", "file_id": "feysh_1", "children":[...]}]}
'''
def dic_string(value, string, dictionary):
if ':\\' in string:
# 处理win平台的分隔符
string = '\\' + string
string = string.replace(':\\', '\\')
string = string.replace('\\', '/')
while string.startswith('/'):
string = string[1:]
parts = string.split('/', 1)
if len(parts) > 1:
z = -1
for i in range(len(dictionary)):
if ('file_label', parts[0]) in dictionary[i].items():
z = i
break
else:
dictionary.append({'file_label': parts[0], 'file_id': f'{parts[0]}_{value}', 'children': []})
dic_string(value, parts[1], dictionary[z]['children'])
else:
dictionary.append({
'file_label': parts[0],
'file_id': value,
})
office = []
for k, v in data.items():
dic_string(k, v, office)
return office

浙公网安备 33010602011771号