简体   繁体   English

从字符串列表中,获取字典列表

[英]From a list of string, get a list of dictionaries

I have a list of strings which contains spanish-recipes´s ingredients and its quantities and I would like to get a list of dictionaries splitting every ingredient, unit and quantity.我有一个字符串列表,其中包含西班牙菜谱的成分及其数量,我想获得一份将每种成分、单位和数量分开的字典列表。

This is the list:这是列表:

ingredients=[
 '50',
 'ccs',
 'aceite',
 '1',
 'hoja',
 'laurel',
 '\n',
 '1',
 'cabeza',
 'ajos',
 '1',
 'vaso',
 'vino',
 '1,5',
 'kilos',
 'conejo',
 '\n',
...]

I would like to get a dict like this:我想得到这样的字典:

my_dic=[
{"name":"aceite" ,"qt":50 ,"unit": "ccs"},
{"name":"laurel" ,"qt":1 ,"unit": "hoja"},
{"name":"ajos" ,"qt":1 ,"unit": "cabeza"},
{"name":"vino" ,"qt":1 ,"unit": "vaso"},
{"name":"conejo" ,"qt":1,5 ,"unit": "kilos"}, 
...]

I have been trying things but it was all a disaster.我一直在尝试,但这一切都是一场灾难。 Any ideas?有任何想法吗?

Thanks in advance!!提前致谢!!

So first, you want to remove the newlines from your original list:因此,首先,您要从原始列表中删除换行符:

ingredients = [i for i in ingredients if i is not '\n']

Then, each ingredient name is every third element in the ingredients list starting from the third element.然后,每个成分名称是ingredients列表中从第三个元素开始的每隔三个元素。 Likewise for the quantity and unit, starting from the second and first elements, respectively:同样对于数量和单位,分别从第二个和第一个元素开始:

names = ingredients[2::3]
units = ingredients[1::3]
qts = ingredients[::3]

Then, iterate through these lists and construct the data structure you specified (which is not actually a dict but a list of dict s):然后,遍历这些列表并构造您指定的数据结构(实际上不是dict而是dictlist ):

my_list = []
for i in range(len(names)):
    my_dict = {"name":names[i],"qt":qts[i],"unit":units[i]}
    my_list.append(my_dict)

There are a lot of ways to compress all of the above, but I have written it for comprehensibility.有很多方法可以压缩以上所有内容,但我写它是为了便于理解。

How about:怎么样:

ingredients = (list)(filter(lambda a: a != '\n', ingredients))
ing_organized = []

for i in range (0, len(ingredients) , 3):
    curr_dict = {"name": ingredients[i+2] ,"qt": ingredients[i] ,"unit": ingredients[i+1]}
    ing_organized.append(curr_dict) 

I just removed '\n' elements from the list as they didn't seem to have meaning.我刚刚从列表中删除了 '\n' 元素,因为它们似乎没有意义。

This doesn't produce a dictionary, but it does give you the output that you specify in the question:这不会产生字典,但它确实为您提供了您在问题中指定的 output:

# Strip out the \n values (can possibly do this with a .strip() in the input stage)
ingredients = [value for value in ingredients if value != '\n']

labels = ['qt', 'unit', 'name']

my_dic = [dict(zip(labels, ingredients[i:i+3])) for i in range(0, len(ingredients), 3)]

my_dic contains: my_dic包含:

[{'qt': '50', 'unit': 'ccs', 'name': 'aceite'},
 {'qt': '1', 'unit': 'hoja', 'name': 'laurel'},
 {'qt': '1', 'unit': 'cabeza', 'name': 'ajos'},
 {'qt': '1', 'unit': 'vaso', 'name': 'vino'},
 {'qt': '1,5', 'unit': 'kilos', 'name': 'conejo'}]

You can clean you list with filter to remove the \n characters and then zip() it together to collect your items together.您可以使用filter清理列表以删除\n字符,然后将其zip()一起收集以将您的项目收集在一起。 This makes a quick two-liner:这使得一个快速的两条线:

l = filter(lambda w: w != '\n', ingredients)

result = [{'name': name, 'qt':qt, 'unit': unit} 
          for qt, unit, name  in zip(l, l, l)]

result:结果:

[{'name': 'aceite', 'qt': '50', 'unit': 'ccs'},
 {'name': 'laurel', 'qt': '1', 'unit': 'hoja'},
 {'name': 'ajos', 'qt': '1', 'unit': 'cabeza'},
 {'name': 'vino', 'qt': '1', 'unit': 'vaso'},
 {'name': 'conejo', 'qt': '1,5', 'unit': 'kilos'}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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