Python 如果我把两个不同的词汇放进一个列表。我得到了错误的结果。一个字典正在覆盖列表中另一个字典的键/值

Python 如果我把两个不同的词汇放进一个列表。我得到了错误的结果。一个字典正在覆盖列表中另一个字典的键/值,python,dictionary,maya,Python,Dictionary,Maya,为了学习,我正在maya中编写自己的索具框架。我的框架的一个基本要素是从场景中收集元数据并将其放入字典中。这样您就可以从该字典重建装备 在一种方法中,我想将两个词典放入一个列表中。但是当我这样做的时候,似乎是在覆盖键的值!就像我会使用字典的更新功能一样 以下是两本词典: {nt.RootOpMetaNode(u'M_ROOT_op_0_METAND'): [{'component_type': u'single_control', 'component_side': u'M', 'main_op

为了学习,我正在maya中编写自己的索具框架。我的框架的一个基本要素是从场景中收集元数据并将其放入字典中。这样您就可以从该字典重建装备

在一种方法中,我想将两个词典放入一个列表中。但是当我这样做的时候,似乎是在覆盖键的值!就像我会使用字典的更新功能一样

以下是两本词典:

{nt.RootOpMetaNode(u'M_ROOT_op_0_METAND'): [{'component_type': u'single_control', 'component_side': u'M', 'main_op_nd_ws_matrix': Matrix([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]), 'sub_op_nd_ws_matrix': [], 'connection_types': [u'translate', u'rotate', u'scale'], 'ik_spaces_ref': None, 'connect_nd': None, 'child_nd': [], 'parent_nd': None, u'control_shape': 0, 'component_index': 0, 'component_name': u'Test'}]}

{nt.RootOpMetaNode(u'M_ROOT_op_0_METAND1'): [{'component_type': u'single_control', 'component_side': u'L', 'main_op_nd_ws_matrix': Matrix([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]), 'sub_op_nd_ws_matrix': [], 'connection_types': [u'translate', u'rotate', u'scale'], 'ik_spaces_ref': None, 'connect_nd': None, 'child_nd': [], 'parent_nd': None, u'control_shape': 0, 'component_index': 1, 'component_name': u'Mom'}]}
在我将它们添加到列表后,这里出现了错误的结果:

[{nt.RootOpMetaNode(u'M_ROOT_op_0_METAND'): [{'component_type': u'single_control', 'component_side': u'L', 'main_op_nd_ws_matrix': Matrix([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]), 'sub_op_nd_ws_matrix': [], 'connection_types': [u'translate', u'rotate', u'scale'], 'ik_spaces_ref': None, 'connect_nd': None, 'child_nd': [], 'parent_nd': None, u'control_shape': 0, 'component_index': 1, 'component_name': u'Mom'}]}, {nt.RootOpMetaNode(u'M_ROOT_op_0_METAND1'): [{'component_type': u'single_control', 'component_side': u'L', 'main_op_nd_ws_matrix': Matrix([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]), 'sub_op_nd_ws_matrix': [], 'connection_types': [u'translate', u'rotate', u'scale'], 'ik_spaces_ref': None, 'connect_nd': None, 'child_nd': [], 'parent_nd': None, u'control_shape': 0, 'component_index': 1, 'component_name': u'Mom'}]}]
以下是创建列表的方法:

def get_operators_meta_data_from_god_node(self):
    """
    Collect the operators meta data from all root meta nodes in the scene.

    Return:
        List: Filled with a dictionary for each root_meta_nd.
        [{"root_meta_nd":pmc.PyNode(), "meta_data":List}]

    """
    result = []
    # get the god meta node from scene.
    self.get_god_meta_nd_from_scene()
    if self.god_meta_nd:
        # get all other meta nodes from scene.
        all_meta_nodes = self.god_meta_nd.list_meta_nodes()
        # get all root meta nodes from all meta nodes.
        all_root_nodes = [
            root
            for root in all_meta_nodes
            if root.attr(constants.META_TYPE).get() == constants.META_TYPE_A
        ]
        # iterate about all root nodes and collect the meta data from it.
        for root_meta_node in all_root_nodes:
            # Gives back a dictionary with meta data from root node.
            temp = self.get_operators_meta_data_from_root_meta_node(
                root_meta_node)
            # Append the meta data dictonary to the result list.
            result.append(temp)
    return result

有人能帮我吗?我快发疯了:-(

欢迎使用StackOverflow!将两个字典放在一个列表中本身不会更改其中任何一个的键或值。代码中肯定有其他内容,可能与您所看到的位置不同。请发布一个最简单的完整示例来说明此问题。顺便说一句,最有可能的解释是您的外部字典(以
{nt.RootOpMetaNode
..开头的字典)引用了相同的内部字典(以
{'component\u type':
..开头的字典)。添加包含导入和给出错误的行的代码,这样我们就可以重现您的错误。很多人都想快速回答!我在描述中加入了生成列表的方法!@JohannesWolz,您更新了答案,您的函数似乎是在类中定义的,当您提出问题时,您应该共享的是最小可复制代码,这意味着如果我复制粘贴问题中的代码,代码只会给出问题的错误信息,而不会给出任何其他信息。在这里,您使用了在类中定义的其他函数,而没有在代码中提及它。