Python 为什么会出现此错误(非类型)?
我知道有关于Python 为什么会出现此错误(非类型)?,python,Python,我知道有关于NoneType错误的帮助论坛。但是为了我的爱,我不能保存我的代码 counter=0 agesList=[] for line in database: newlist=database[counter].split(',') counter=counter+1 age=newlist[-2] agesList=agesList.append(age) #print(age) print(agesList) 我有一个由逗号分隔的值组成的文档
NoneType
错误的帮助论坛。但是为了我的爱,我不能保存我的代码
counter=0
agesList=[]
for line in database:
newlist=database[counter].split(',')
counter=counter+1
age=newlist[-2]
agesList=agesList.append(age)
#print(age)
print(agesList)
我有一个由逗号分隔的值组成的文档,我将它们都分割到自己的列表中,并取出我要查找的编号,该编号位于新列表
的倒数第二位。如果我打印年龄,我会在整个循环中得到所有正确的年龄。但如果我试图将所有年龄数字附加到我的空白列表中,我会出现以下错误:
agesList=agesList.append(age)
AttributeError: 'NoneType' object has no attribute 'append'
我能得到一些关于我为什么会出现这个错误的帮助吗?谢谢您正在为列表分配一个函数调用,这就是您正在覆盖列表的原因 不要这样做:
agesList=agesList.append(age)
只需这样做:
agesList.append(age)
您正在为列表分配函数调用,这就是您覆盖列表的原因 不要这样做:
agesList=agesList.append(age)
只需这样做:
agesList.append(age)
不要使用
agesList=agesList.append(age)
仅使用agesList.append(age)
因为list.append()
返回None
并覆盖列表
因此,for循环的第一次迭代将正常运行,之后
agesList
为None
,您将得到您提到的错误。而不是agesList=agesList.append(age)
仅使用agesList.append(age)
因为list.append()
返回None
并覆盖列表
因此,for循环的第一次迭代将正常运行,之后agesList
为None
,您将得到您提到的错误