Вы можете попробовать это на основе списка match
:
import re
output = ['labels: imagenet_labels.txt \n', '\n', 'Model: efficientnet-edgetpu-S_quant_edgetpu.tflite \n', '\n', 'Image: img0000.jpg \n', '\n', '----INFERENCE TIME----\n', 'Note: The first inference on Edge TPU is slow because it includes loading the model into Edge TPU memory.\n', 'time: 6.0ms\n', '-------RESULTS--------\n','results: wall clock\n', 'score: 0.25781\n', '##################################### \n', ' \n', '\n']
mydict = {}
regex1 = re.compile(fr'(\w+:)\s(.*)')
match_regex1 = list(filter(regex1.match, output))
match = [line.rstrip('\n') for line in match_regex1]
features_wanted='ModelImagetimeresultsscore'
dct={i.replace(' ','').split(':')[0]:i.replace(' ','').split(':')[1] for i in match if i.replace(' ','').split(':')[0] in features_wanted}
mydict=dct
print(dct)
Вывод:
{'Model': 'efficientnet-edgetpu-S_quant_edgetpu.tflite', 'Image': 'img0000.jpg', 'time': '6.0ms', 'results': 'wallclock', 'score': '0.25781'}
Объяснение dct: Это Понимание словаря и перебирает совпадение со списком, поэтому вот пример итерации с 'Model: efficientnet-edgetpu-S_quant_edgetpu.tflite'
:
#First check if it is a feature wanted:
i='Model: efficientnet-edgetpu-S_quant_edgetpu.tflite'
i.replace(' ','')
>>>'Model:efficientnet-edgetpu-S_quant_edgetpu.tflite'
i.replace(' ','').split(':')
>>>['Model','efficientnet-edgetpu-S_quant_edgetpu.tflite']
i.replace(' ','').split(':')[0] in features_wanted #'Model' in 'ModelImagetimeresultsscore'
>>>True
#If it is in features_wanted, an item like this is append to the dictionary:
i.replace(' ','').split(':')[0]:i.replace(' ','').split(':')[1]
>>>'Model':'efficientnet-edgetpu-S_quant_edgetpu.tflite'