Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.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_Python 2.7_Dictionary - Fatal编程技术网

Python数据结构中可能存在的错误

Python数据结构中可能存在的错误,python,python-2.7,dictionary,Python,Python 2.7,Dictionary,Python删除嵌套字典的内容似乎有问题。下面是第二个实体的数据结构: body = { 'Violations' : [ {'foo': 7, 'bar': 8, 'baz': 9}, {'tez': 2, 'rez': 4, '

Python删除嵌套字典的内容似乎有问题。下面是第二个实体的数据结构:

body = {
    'Violations' : [
                     {'foo': 7,
                      'bar': 8,
                      'baz': 9},
                     {'tez': 2,
                      'rez': 4,
                      'sez': 6}
                   ]
}
给出了body的数据结构。然后我将它嵌套在最后一个字典中(与“outdict”关联)

print语句的输出如下所示:

another check: 8
more checks: 0
不知何故,在第一个和第二个print语句之间,缺少8个值。我们可以看出,这并不是由于调用了一个函数。当我分配与outdict关联的数据结构时,似乎会发生这种情况

这个问题不仅仅局限于使用字典。以前,我尝试过“outlist”,它附加了一个包含四个字典的元组。这引起了同样的问题

真的不知道这里发生了什么。请让我知道你的想法

使用Python2.7.8、Fedora21、VirtualEnvrapper

只是试图用一个单独的脚本来复制这个问题。看起来是这样的:

#! /usr/bin/python

body = {}
body['Violations'] = []
body['Violations'].append({'foo': 7,
                           'bar': 8,
                           'baz': 9})
body['Violations'].append({'tez': 2,
                           'rez': 4,
                           'sez': 6})


print "another check: %d " % len(body['Violations'])

outdict = {}

outdict[1] = {'index': 'foo',
              'doc_type': 'doc',
              'id': 1,
              'body': body}

print "more checks: %d " % len(body['Violations'])
此代码无法复制该问题。所以现在我真的怀疑是否存在与数据结构相关的bug。更可能的情况是,我在某个地方出现语法错误,该错误正在失败,但没有被提出


这里有一个精确的代码,它给了我这个问题。打印报表在那里。让我知道你的想法/如果你能复制。谢谢

代码与输出不一致。具体来说,
len(body['inflights'])
2
。你的
body
的长度永远不会是8。它是2(因为它包含两个字典)。代码的其余部分没有意义。什么是输出?什么是
\u id
?什么是第二个正文?请给出一个完整的例子来说明这个问题。这是真的。我所写的示例只包括两项。我在我的机器上使用的数据。我有8件物品。为了清楚起见,我将编辑这个问题。感谢您的代码中存在bug的可能性>Python的数据结构实现中存在bug的可能性,尽管我知道这有时会令人沮丧,而且很容易责怪您以外的人:)@qwwqwq事实上,我会说
Pbug(self)>>Pbug(Py)
#! /usr/bin/python

body = {}
body['Violations'] = []
body['Violations'].append({'foo': 7,
                           'bar': 8,
                           'baz': 9})
body['Violations'].append({'tez': 2,
                           'rez': 4,
                           'sez': 6})


print "another check: %d " % len(body['Violations'])

outdict = {}

outdict[1] = {'index': 'foo',
              'doc_type': 'doc',
              'id': 1,
              'body': body}

print "more checks: %d " % len(body['Violations'])