如何使用python将列表的一些值放入字典中?
我有一个清单(见清单),我想把它送到字典里 但我不想发送所有数据。只是一些值(请参见一些值/特性),这些值会重复多次。例如,“Model:xxx”一词出现了7次。“xxx”是模型的名称,它将更改 到目前为止,我只能在字典中输入列表中的最后一个值。如何将列表中的所有值放入字典 一些价值观: 标签:xxxx 型号:xxxx 图片:xxxx 推断:xxxx 分数:xxxx TPU_温度(°C):xxxx 时间(毫秒):xxx--有两个,我不知道是否可以只提取第二个。但如果不是,也没问题。两者都提取就可以了-- 这是代码-尝试1如何使用python将列表的一些值放入字典中?,python,regex,list,dictionary,Python,Regex,List,Dictionary,我有一个清单(见清单),我想把它送到字典里 但我不想发送所有数据。只是一些值(请参见一些值/特性),这些值会重复多次。例如,“Model:xxx”一词出现了7次。“xxx”是模型的名称,它将更改 到目前为止,我只能在字典中输入列表中的最后一个值。如何将列表中的所有值放入字典 一些价值观: 标签:xxxx 型号:xxxx 图片:xxxx 推断:xxxx 分数:xxxx TPU_温度(°C):xxxx 时间(毫秒):xxx--有两个,我不知道是否可以只提取第二个。但如果不是,也没问题。两者都提取就可
#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')
这是我用代码得到的字典-尝试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')
仅显示列表的最后一个值
这是代码-尝试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)))
这是我在代码尝试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)))
在这里,我把所有的清单都输入字典。但我没有删除空格,也没有将数据划分为“key:value”
列表(原始列表如下所示)
这是列表(因此您可以进行测试)
如果我正确理解了您的问题(理解您真正想要的是什么有点困难),这段代码将很高兴地将所有数据放入列表中:
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))
打印出来
{'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 ']}
但是,如果行的顺序很重要(例如,每个“标签:”开始一个新的组),那么很可能您想要类似的东西
# 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)
输出
[{'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 '}]
这个错误不是来自你发布的代码。你是对的!下面我有一些未注释的单词。@Aizzaac
fr
是一个原始的f字符串。f字符串用于将变量替换为字符串,这里不需要。您可以使用defaultdict(list)
和附加值,但不确定它是否是您要查找的。同一个键在字典中不能有两次。选项2似乎是正确的。我的目标是将数据发送到elasticearch。我将测试它是否有效。它似乎有效。这是我放置结果的另一个链接:https://stackoverflow.com/questions/62459572/how-can-i-read-data-from-a-list-and-index-specific-values-into-elasticsearch-us
添加了注释,希望对您有所帮助。