【发布时间】:2016-07-18 13:46:00
【问题描述】:
我的场景是这样的:
pic_from_list = ['best', 'good', 'ok']
dict_list = [{id = "1", food_source : "this food;is;good"}, {id = "2", food_source : "this food;is;ok"}, {id = "3", food_source : "this food is the best"}, {id = "4", food_source : none}, {id = "5", food_source : "no source"}]
我的代码是这样的:
for item in dict_list:
idt = item['id']
#print(idt)
source = item['source']
#print(source)
words = re.split(r'[.|;]+', str(source))
#print(words)
class_type = [i.strip() for i in words if i in pic_from_list]
#print(class_type)
if len(class_type) ==0:
if idt not in new_dict.values():
new_dict.update({'id': idt , 'food_class': 'none'})
dict_list.append(new_dict)
else:
pass
elif:
if idt not in new_dict.values():
new_dict.update({'id': idt , 'food_class': 'none'})
dict_list.append(new_dict)
else:
pass
print(dict_list[:4])
这给了我错误的输入,如:
[{'id':1, food_class:'none'}, {'id':1, food_class:'good'}, {'id':1, food_class:'ok'}..]
【问题讨论】:
-
请缩进您的代码。
-
请将其缩减为 minimal reproducible example 并解释您的预期。
标签: python dictionary