Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将值从嵌套字典获取到列表_Python - Fatal编程技术网

Python 将值从嵌套字典获取到列表

Python 将值从嵌套字典获取到列表,python,Python,很奇怪,以前没有人问这个问题。我在网上找不到任何答案。我有一个嵌套的词汇表,我想要一个所有it值的列表(不是嵌套列表)。这是我的代码: dico = { "Balance": { "Normal": { "P1x": 0.889, "P1y": 700.0, "P2x": 0.889, "P2y": 884.0, "P3x": 1.028,

很奇怪,以前没有人问这个问题。我在网上找不到任何答案。我有一个嵌套的词汇表,我想要一个所有it值的列表(不是嵌套列表)。这是我的代码:

dico = {
    "Balance": {
        "Normal": {
            "P1x": 0.889,
            "P1y": 700.0,
            "P2x": 0.889,
            "P2y": 884.0,
            "P3x": 1.028,
            "P3y": 1157.0,
            "P4x": 1.201,
            "P4y": 1157.0,
            "P5x": 1.201,
            "P5y": 700.0
        },
        "Utility": {
            "P1x": 0.889,
            "P1y": 700.0,
            "P2x": 0.889,
            "P2y": 884.0,
            "P3x": 0.947,
            "P3y": 998.0,
            "P4x": 1.028,
            "P4y": 998.0,
            "P5x": 1.028,
            "P5y": 700.0,
        }
    }
}

def grab_children(father):
    local_list = []
    for key, value in father.items():
        local_list.append(value)
        local_list.extend(grab_children(father[key]))
    return local_list

print(grab_children(dico))
字串通常更长,包含字符串、布尔值、整数和浮点数。
当我尝试我的函数时,它说
AttributeError:'str'对象没有属性'items'

我知道为什么,但我不知道如何修复它。。。你能帮我吗?
谢谢

您可以尝试:

import collections

def walk(node):
    for key, item in node.items():
        if isinstance(item, collections.Mapping):
            print(key)
            walk(item)
        else:
            print('\t',key, item)
以您的示例为例,打印:

Balance
Utility
     P3y 998.0
     P1x 0.889
     P5x 1.028
     P5y 700.0
     P2x 0.889
     P1y 700.0
     P2y 884.0
     P4x 1.028
     P3x 0.947
     P4y 998.0
Normal
     P3y 1157.0
     P1x 0.889
     P5x 1.201
     P5y 700.0
     P2x 0.889
     P1y 700.0
     P2y 884.0
     P4x 1.201
     P3x 1.028
     P4y 1157.0
在Python 3.3+下,您可以执行以下操作:

def walk(node):
    for key, value in node.items():
        if isinstance(value, collections.Mapping):
            yield from walk(value)
        else:
            yield key, value 

>>> list(walk(dico))
[('P5y', 700.0), ('P2y', 884.0), ('P4y', 1157.0), ('P4x', 1.201), ('P1x', 0.889), ('P3y', 1157.0), ('P2x', 0.889), ('P1y', 700.0), ('P3x', 1.028), ('P5x', 1.201), ('P5y', 700.0), ('P2y', 884.0), ('P4y', 998.0), ('P4x', 1.028), ('P1x', 0.889), ('P3y', 998.0), ('P2x', 0.889), ('P1y', 700.0), ('P3x', 0.947), ('P5x', 1.028)]
然后,如果您只需要这些值:

def walk(node):
    for key, value in node.items():
        if isinstance(value, collections.Mapping):
            yield from walk(value)
        else:
            yield value    

>>> list(walk(dico))
[700.0, 0.889, 0.889, 998.0, 1.028, 0.947, 700.0, 884.0, 998.0, 1.028, 700.0, 0.889, 0.889, 1157.0, 1.201, 1.028, 700.0, 884.0, 1157.0, 1.201]

但是,请记住,Python dict没有顺序,因此值列表中的顺序与提供给它的dict具有相同的无意义顺序

您的递归是错误的,因为它总是以错误告终:当值是一个字符串而不是另一个嵌套dict时,您仍然将其传递给
grab\u children()
。当值不是dict时,应该停止递归。哦,是的,这是真的,Python dict没有任何顺序。。。所以把这些值放在列表上没有任何意义。。。无论如何谢谢你!获取所有值确实有其用途,例如测试所有嵌套dict中是否存在值。例如,您可以构建一组dict
VAL=set(walk(dict))
,并能够在VAL中测试
某些东西。
def walk(node):
    for key, value in node.items():
        if isinstance(value, collections.Mapping):
            yield from walk(value)
        else:
            yield value    

>>> list(walk(dico))
[700.0, 0.889, 0.889, 998.0, 1.028, 0.947, 700.0, 884.0, 998.0, 1.028, 700.0, 0.889, 0.889, 1157.0, 1.201, 1.028, 700.0, 884.0, 1157.0, 1.201]