Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.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
在Dict Python中访问列表中的值_Python_Django - Fatal编程技术网

在Dict Python中访问列表中的值

在Dict Python中访问列表中的值,python,django,Python,Django,我在django有一个表单集,它有多个表单,我保存为: manifestData=form.cleanned\u data 如果Iprint(manifestdata)它将返回以下内容: 我需要访问每个ProductCode,然后从列表中弹出它。因此,在我看来,我正在尝试以下方法: ... for item in manifestData: x = manifestData.pop['ProductCode'] #this is highlighted in the error mes

我在django有一个表单集,它有多个表单,我保存为:

manifestData=form.cleanned\u data

如果I
print(manifestdata)
它将返回以下内容:

我需要访问每个
ProductCode
,然后从列表中弹出它。因此,在我看来,我正在尝试以下方法:

...

for item in manifestData:
   x = manifestData.pop['ProductCode'] #this is highlighted in the error message

...

当我这样做时,我得到一个TypeError,读取“需要一个整数”。有人能向我解释一下/我如何处理这个问题吗?

在您的代码中,manifestData是一个字典列表。在for循环中,您在列表中循环以获取每个字典,但随后您尝试从manifestData而不是项中弹出

将代码更改为:

...

for item in manifestData:
   x = item.pop('ProductCode') #pop from item, not manifestData

...

注意:对于pop(),您需要在代码中使用括号,而不是括号,manifestData是字典列表。在for循环中,您在列表中循环以获取每个字典,但随后您尝试从manifestData而不是项中弹出

将代码更改为:

...

for item in manifestData:
   x = item.pop('ProductCode') #pop from item, not manifestData

...

注意:对于pop(),您需要使用括号,而不是括号。您需要从项中弹出,而不是从数据中弹出。
2.在对字典使用pop函数而不是[]时使用()括号

for item in manifestData:
   x = item.pop('ProductCode') #to pop from dictionary you need to use this

1.您需要从项目中弹出,而不是从数据中弹出。
2.在对字典使用pop函数而不是[]时使用()括号

for item in manifestData:
   x = item.pop('ProductCode') #to pop from dictionary you need to use this