Python:TypeError:'NoneType'对象不可迭代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34616870/
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
Python: TypeError: 'NoneType' object is not iterable
提问by Student1001
I am attempting to use the following function to simulate loads on a beam:
我正在尝试使用以下函数来模拟梁上的载荷:
def simulateBeamRun(personList, beam, times):
I have come up with the following code so far:
到目前为止,我已经提出了以下代码:
def createPersonList(fileName):
"""Function will go through each line of file and
create a person object using the data provided in
the line and add it to a list
"""
theFile = open(fileName)
next(theFile)
#array = []
for line in theFile:
aList = line.split(',')
bList = map(lambda s: s.strip('\n'), aList)
cList = [float(i) for i in bList]
print cList
def simulateBeamRun(personList, beam, times):
"""Takes a list of times covering the duration of
the simulation (0-35 s), the list of person
objects and a beam object to simulate a beam run
"""
dList = []
for time in times:
eList = []
for person in personList:
loadTuples = personModel.person.loadDisplacement(time)
if beamModel.beam.L > loadTuples[1] > 0:
eList.append(loadTuples)
else:
return None
beamModel.beam.setLoads(eList)
dList.append(beamModel.beam.getMaxDeflection())
However, I am getting the following error when trying to run the function (before I give it any inputs:
但是,在尝试运行该函数时出现以下错误(在我给它任何输入之前:
for person in personList:
TypeError: 'NoneType' object is not iterable
回答by Erica
In order to be iterated, personList
needs to have some values in it.
为了被迭代,personList
需要在其中包含一些值。
If you're creating personList
with the function createPersonList
, then you need to return a value. Otherwise, that list doesn't exist outside of createPersonList
.
如果您使用personList
function创建createPersonList
,那么您需要返回一个 value。否则,该列表不存在于createPersonList
.
def createPersonList(fileName):
# do stuff to create cList
return cList
personList = createPersonList(myFile)
Then, personList
will have values and you can use it in subsequent functions.
然后,personList
将有值,您可以在后续函数中使用它。
simulateBeamRun(personList, beam, times)
If you want to avoid running that loop at all in cases where personList
doesn't have values, include a conditional.
如果您想在personList
没有值的情况下完全避免运行该循环,请包含一个条件。
if personList is None:
print "No values in personList"
else:
for person in personList:
# do stuff with person
回答by BAE
May the following code help
可能以下代码有帮助
def createPersonList(fileName):
"""Function will go through each line of file and
create a person object using the data provided in
the line and add it to a list"""
cList=[]#see my comments. if the following loop not happen, still return []
theFile = open(fileName)
next(theFile)
#array = []
for line in theFile:
aList = line.split(',')
bList = map(lambda s: s.strip('\n'), aList)
cList += [float(i) for i in bList]# if bList is iterable, [float(i) for i in bList] should be a list (including [])
return cList#according to your comments, should return here.
float(i) may throw errors, so use try-except. I think checking related to personList should be done in this function, error info should be logged.
float(i) 可能会抛出错误,因此请使用 try-except。我认为应该在这个函数中完成与 personList 相关的检查,应该记录错误信息。