简体   繁体   English

使用 Python 中的嵌套列表从字典列表中解析值

[英]Parsing values from a list of dictionaries with nested list in Python

I have a list of dictionaries with info in such format:我有一个包含这种格式信息的字典列表:

p_mat_list = [
    {'name': 'Mirror', 'link': '/somelink1/'},
    {'name': 'Gold Leaf', 'link': '/somelink2/'}
]

First, create python list with all name values:首先,使用所有名称值创建 python 列表:

product_materials = []
for material in p_mat_list:
    product_materials.append(material['name'])

However, I don't know what would be best way to get all name values when list will have nested list(s) with dictionaries alongside, as below:但是,当列表将嵌套列表与字典并列时,我不知道获取所有名称值的最佳方法是什么,如下所示:

p_mat_list = [
    [{'name': 'Painted'}, {'name': 'Wood'}],
    {'name': 'Mirror'},
    {'name': 'Gold Leaf'}
]

How can I get all these name values: Painted , Wood , Mirror , Gold Leaf ?我如何获得所有这些名称值: PaintedWoodMirrorGold Leaf

Also, how could I approach merging name values from all dictionaries inside every nested list into one value and then get that into list with others, so would get such values: Painted Wood, Mirror, Gold Leaf.另外,我如何将每个嵌套列表中的所有字典中的名称值合并为一个值,然后将其与其他值一起放入列表中,从而获得以下值:Painted Wood、Mirror、Gold Leaf。

There won't be lists nested more levels and no more than two dictionaries inside them, so for eg from this list below would need to get such values: Painted Wood, Mirror, Varnished Wood, Gold Leaf.不会有嵌套更多级别的列表,其中不会有超过两个字典,因此例如从下面的列表中需要获得这样的值:Painted Wood、Mirror、Varnished Wood、Gold Leaf。

p_mat_list = [
[{'name': 'Painted'}, {'name': 'Wood'}],
{'name': 'Mirror'},
[{'name': 'Varnished'}, {'name': 'Wood'}],
{'name': 'Gold Leaf'}
]

If you're open to bringing in an external library, you could use the collapse() function from more-itertools .如果您愿意引入外部库,则可以使用more-itertools 中collapse()函数 Use it something like this:像这样使用它:

import more_itertools as mt
list(mt.collapse(p_mat_list, base_type=dict))
    [{'name': 'Painted'},
     {'name': 'Wood'},
     {'name': 'Mirror'},
     {'name': 'Gold Leaf'}]

Then in your case, you can just extract the value corresponding to 'name' from each dict, instead of making a list of them.然后在您的情况下,您可以从每个字典中提取与'name'对应的值,而不是列出它们。

>>> [d['name'] for d in mt.collapse(p_mat_list, base_type=dict)]

This has the advantage that you don't have to worry about how many levels of list nesting there are.这样做的好处是您不必担心有多少级列表嵌套。

The best way would be to flatten the complex list you have a function like this :最好的方法是展平你有这样一个功能的复杂列表:

def flatten(x):
    if isinstance(x, dict) :
        return [x]
    elif isinstance(x, collections.Iterable) :
        return [a for i in x for a in flatten(i)]
    else:
        return [x]

This function, takes your p_mat_list as an argument and returns a single list of dictionaries.此函数将您的p_mat_list作为参数并返回一个字典列表。

get_list = flatten(p_mat_list)

product_materials = []
for material in get_list :
    product_materials.append(material['name'])

Your product_materials list :您的product_materials列表:

['Painted', 'Wood', 'Mirror', 'Gold Leaf']

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

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