本文介绍了追加将我的列表转到NoneType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python Shell中,我输入:

In Python Shell, I entered:

aList = ['a', 'b', 'c', 'd']  
for i in aList:  
    print(i)

得到了

a  
b  
c  
d  

但是当我尝试时:

aList = ['a', 'b', 'c', 'd']  
aList = aList.append('e')  
for i in aList:  
    print(i) 

得到了

Traceback (most recent call last):  
  File "<pyshell#22>", line 1, in <module>  
    for i in aList:  
TypeError: 'NoneType' object is not iterable  

有人知道发生了什么吗?我该如何解决/解决它?

Does anyone know what's going on? How can I fix/get around it?

推荐答案

list.append是一种修改现有列表的方法.它不返回新列表,而是返回None,就像大多数修改列表的方法一样.只需执行aList.append('e'),您的列表就会添加元素.

list.append is a method that modifies the existing list. It doesn't return a new list -- it returns None, like most methods that modify the list. Simply do aList.append('e') and your list will get the element appended.

这篇关于追加将我的列表转到NoneType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 21:51