简体   繁体   中英

How to put some of the values of a list inside a dictionary, using python?

I have a list (see LIST) that I want to send to a dictionary.

But I do not want to send all the data. Just some values (see SOME VALUES/FEATURES) which happen to repeat many times. For example, the word "Model: xxx" appears like 7 times. "xxx" is the name of the model and it will change.

So far I can only put in the dictionary the last values of the list. How can I put all the values from the list into the dictionary?

SOME VALUES:

Labels: xxxx

Model: xxxx

Image: xxxx

Inference: xxxx

Score: xxxx

TPU_temp(°C): xxxx

Time(ms): xxx ---There are 2 of these, I do not know if it is possible to extract ONLY the second one. But if not, no problem. Extracting both will be fine.--

THIS IS THE CODE - ATTEMPT 1

#this is to match tha values/features that I want to extract
regex = re.compile(r'(\w+)\((.+)\):\s(.*)|(\w+:)\s(.*)')
match_regex = list(filter(regex.match, output))
match = [line.rstrip('\n') for line in match_regex]

features_wanted='ModelImageTime(ms)InferenceScoreTPU_temp(°C)'


#Removing whitespaces and splitting data into "key:value"
#Sending the values/features into a dictionary
dct={i.replace(' ','').split(':')[0]:i.replace(' ','').split(':')[1] for i in match if i.replace(' ','').split(':')[0] in features_wanted}
print(dct, '\n')

THIS IS THE DICTIONARY THAT I GET WITH MY CODE - ATTEMPT 1

Only the last value of the list appears.

在此处输入图像描述

THIS IS THE CODE - ATTEMPT 2

regex = re.compile(r'(\w+)\((.+)\):\s(.*)|(\w+:)\s(.*)')
match_regex = list(filter(regex.match, data))
match = [line.rstrip('\n') for line in match_regex]

dixie=dict(list(enumerate(match)))

THIS IS THE DICTIONARY THAT I GET WITH MY CODE - ATTEMPT 2

Here I am sending all the list into the dictionary. But I haven't removed whitespaces neither divided data into "key:value" 在此处输入图像描述

LIST (original list looks like this)

在此处输入图像描述

THIS IS THE LIST (so you can test)

[
    "labels: imagenet_labels.txt ",
    "Model: efficientnet-edgetpu-S_quant_edgetpu.tflite ",
    "Image: insect.jpg ",
    "Time(ms): 23.1",
    "Time(ms): 5.7",
    "Inference: corkscrew, bottle screw",
    "Score: 0.03125 ",
    "TPU_temp(°C): 57.05",
    "labels: imagenet_labels.txt ",
    "Model: efficientnet-edgetpu-M_quant_edgetpu.tflite ",
    "Image: insect.jpg ",
    "Time(ms): 29.3",
    "Time(ms): 10.8",
    "Inference: dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk",
    "Score: 0.09375 ",
    "TPU_temp(°C): 56.8",
    "labels: imagenet_labels.txt ",
    "Model: efficientnet-edgetpu-L_quant_edgetpu.tflite ",
    "Image: insect.jpg ",
    "Time(ms): 45.6",
    "Time(ms): 31.0",
    "Inference: pick, plectrum, plectron",
    "Score: 0.09766 ",
    "TPU_temp(°C): 57.55",
    "labels: imagenet_labels.txt ",
    "Model: inception_v3_299_quant_edgetpu.tflite ",
    "Image: insect.jpg ",
    "Time(ms): 68.8",
    "Time(ms): 51.3",
    "Inference: ringlet, ringlet butterfly",
    "Score: 0.48047 ",
    "TPU_temp(°C): 57.3",
    "labels: imagenet_labels.txt ",
    "Model: inception_v4_299_quant_edgetpu.tflite ",
    "Image: insect.jpg ",
    "Time(ms): 121.8",
    "Time(ms): 101.2",
    "Inference: admiral",
    "Score: 0.59375 ",
    "TPU_temp(°C): 57.05",
    "labels: imagenet_labels.txt ",
    "Model: inception_v2_224_quant_edgetpu.tflite ",
    "Image: insect.jpg ",
    "Time(ms): 34.3",
    "Time(ms): 16.6",
    "Inference: lycaenid, lycaenid butterfly",
    "Score: 0.41406 ",
    "TPU_temp(°C): 57.3",
    "labels: imagenet_labels.txt ",
    "Model: mobilenet_v2_1.0_224_quant_edgetpu.tflite ",
    "Image: insect.jpg ",
    "Time(ms): 14.4",
    "Time(ms): 3.3",
    "Inference: leatherback turtle, leatherback, leathery turtle, Dermochelys coriacea",
    "Score: 0.36328 ",
    "TPU_temp(°C): 57.3",
    "labels: imagenet_labels.txt ",
    "Model: mobilenet_v1_1.0_224_quant_edgetpu.tflite ",
    "Image: insect.jpg ",
    "Time(ms): 14.5",
    "Time(ms): 3.0",
    "Inference: bow tie, bow-tie, bowtie",
    "Score: 0.33984 ",
    "TPU_temp(°C): 57.3",
    "labels: imagenet_labels.txt ",
    "Model: inception_v1_224_quant_edgetpu.tflite ",
    "Image: insect.jpg ",
    "Time(ms): 21.2",
    "Time(ms): 3.6",
    "Inference: pick, plectrum, plectron",
    "Score: 0.17578 ",
    "TPU_temp(°C): 57.3",
]

If I understood your question correctly (it's a little hard to understand what you're really looking for), this code will happily put all of the data into a dict-of-lists:

from pprint import pprint
from collections import defaultdict

lines = [
    "labels: imagenet_labels.txt ",
    "Model: efficientnet-edgetpu-S_quant_edgetpu.tflite ",
    "Image: insect.jpg ",
    "Time(ms): 23.1",
    "Time(ms): 5.7",
    "Inference: corkscrew, bottle screw",
    "Score: 0.03125 ",
    "TPU_temp(°C): 57.05",
    "labels: imagenet_labels.txt ",
    "Model: efficientnet-edgetpu-M_quant_edgetpu.tflite ",
    "Image: insect.jpg ",
    "Time(ms): 29.3",
    "Time(ms): 10.8",
    "Inference: dragonfly, darning needle, devil's darning needle, sewing needle, snake feeder, snake doctor, mosquito hawk, skeeter hawk",
    "Score: 0.09375 ",
    "TPU_temp(°C): 56.8",
    "labels: imagenet_labels.txt ",
    "Model: efficientnet-edgetpu-L_quant_edgetpu.tflite ",
    "Image: insect.jpg ",
    "Time(ms): 45.6",
    "Time(ms): 31.0",
    "Inference: pick, plectrum, plectron",
    "Score: 0.09766 ",
]

values = defaultdict(list)

for line in lines:
    key, value = line.split(": ", 1)
    values[key].append(value)

pprint(dict(values))

prints out

{'Image': ['insect.jpg ', 'insect.jpg ', 'insect.jpg '],
 'Inference': ['corkscrew, bottle screw',
               "dragonfly, darning needle, devil's darning needle, sewing "
               'needle, snake feeder, snake doctor, mosquito hawk, skeeter '
               'hawk',
               'pick, plectrum, plectron'],
 'Model': ['efficientnet-edgetpu-S_quant_edgetpu.tflite ',
           'efficientnet-edgetpu-M_quant_edgetpu.tflite ',
           'efficientnet-edgetpu-L_quant_edgetpu.tflite '],
 'Score': ['0.03125 ', '0.09375 ', '0.09766 '],
 'TPU_temp(°C)': ['57.05', '56.8'],
 'Time(ms)': ['23.1', '5.7', '29.3', '10.8', '45.6', '31.0'],
 'labels': ['imagenet_labels.txt ',
            'imagenet_labels.txt ',
            'imagenet_labels.txt ']}

However, if the order of the lines matters (eg each "labels: " starts a new group), chances are you want something like

# Initialize our list of groups; add in an empty group
# to make the future code easier.
groups = [{}]

for line in lines:
    # Split each line in 2 parts (1 split) on `: `
    key, value = line.split(": ", 1)
    if key == "labels":  # If the key is labels, it starts a new group.
        if groups[-1]:  # If there is something in the current (last) group,
            groups.append({})  # ... add a new one.
    # No matter what, add the key-value pair to the last group.
    groups[-1][key] = value

pprint(groups)

to output

[{'Image': 'insect.jpg ',
  'Inference': 'corkscrew, bottle screw',
  'Model': 'efficientnet-edgetpu-S_quant_edgetpu.tflite ',
  'Score': '0.03125 ',
  'TPU_temp(°C)': '57.05',
  'Time(ms)': '5.7',
  'labels': 'imagenet_labels.txt '},
 {'Image': 'insect.jpg ',
  'Inference': "dragonfly, darning needle, devil's darning needle, sewing "
               'needle, snake feeder, snake doctor, mosquito hawk, skeeter '
               'hawk',
  'Model': 'efficientnet-edgetpu-M_quant_edgetpu.tflite ',
  'Score': '0.09375 ',
  'TPU_temp(°C)': '56.8',
  'Time(ms)': '10.8',
  'labels': 'imagenet_labels.txt '},
 {'Image': 'insect.jpg ',
  'Inference': 'pick, plectrum, plectron',
  'Model': 'efficientnet-edgetpu-L_quant_edgetpu.tflite ',
  'Score': '0.09766 ',
  'Time(ms)': '31.0',
  'labels': 'imagenet_labels.txt '}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM