Python 类型错误:描述符“追加”需要“列表”对象但收到“字典”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39677212/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 22:34:18  来源:igfitidea点击:

TypeError: descriptor 'append' requires a 'list' object but received a 'dict'

python

提问by arjun

In the for loop I have a dictionary object like this:

在 for 循环中,我有一个像这样的字典对象:

mob1 = {
    "Item": item1,
    'Price': price1,
    'Desc': desc1
}

and I tried to append it like:

我尝试将其附加如下:

list.append(mob1)

I get the following error:

我收到以下错误:

Traceback (most recent call last):
  File "/home/turbolab/Documents/python_test/Sep 23 data_to_json test.json", line 32, in <module>
    list.append(mob1)
TypeError: descriptor 'append' requires a 'list' object but received a 'dict'

回答by zvone

listis a class. appendis a method of that class which has to be called on instances of list.

list是一个类。append是该类的一个方法,必须在 的实例上调用list

list.append(7)  # error

mylist = list()
mylist.append(7)  # ok

回答by mengg

Have you tried list.append([mob1['Item'],mob1['Price'],mob1['Desc']])?

你试过list.append([mob1['Item'],mob1['Price'],mob1['Desc']])吗?