Hi So I created this function called runloopg(x,y,z) that produces a list but I can't call an item on the list:
我创建了一个名为runloopg(x,y,z)的函数,它产生一个列表,但我不能在列表上调用一个项目:
p=runloopg(10,0.1,6)
<generator object rtpairs at 0x000000000BAF1F78>
[(0,0,0,0,0,1), (0.01,0,0,0,0,1), (0.0062349,0.00781831,0,0,0,1), (-0.00222521,0.00974928,0,0,0,1), (-0.00900969,0.00433884,0,0,0,1), (0.0549583,-0.0712712,0,0,0,1), (0.0627244,-0.0645419,0,0,0,1), (0.0696727,-0.0569711,0,0,0,1), (0.0757128,-0.0486577,0,0,0,1), (0.0807659,-0.0397099,0,0,0,1), (0.084766,-0.0302444,0,0,0,1), (0.0876611,-0.0203847,0,0,0,1), (0.0894134,-0.0102592,0,0,0,1)]
However when I call an item on my list as so:
然而,当我在我的列表上调用一个项目时:
p[0]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-240-a69904524460> in <module>()
----> 1 p[0]
TypeError: 'NoneType' object has no attribute '__getitem__'
This is the code for runloopg:
这是runloopg的代码:
import numpy
import raytracer29
def rtpairs(R, N):
for i in range(len(R)):
r=R[i]
n=N[i]
for j in range(n):
t = j*2*numpy.pi/n
yield r,t
def rtuniform(n, rmax, m):
R=numpy.arange(0,rmax,rmax/n)
N=numpy.arange(1, n*m, m)
return rtpairs(R, N)
def runloopg(n, rmax, m):
#print rtuniform(n, rmax, m)
bund = []
for r,t in rtuniform(n, rmax, m):
myRay = raytracer29.Ray(r * numpy.cos(t), r * numpy.sin(t),0,0,0,1)
bund.append(myRay)
return bund
4 个解决方案
#1
-2
A generator object is returned. Once you used a generator, it's exhausted and you can't use it again.
返回一个生成器对象。一旦你使用了发电机,它就耗尽了,你不能再用它了。
But you can take all of it's values as a list:
但是你可以把它的所有值作为一个列表:
p = list(runloopg(10,0.1,6))
#2
1
You didn't post the relevant code - your function's definition - but very obviously this function returns None
.
你没有发布相关的代码——你的函数的定义——但是很明显这个函数没有返回。
edit: Ok from the snippet you posted runloopg
does indeed return a list so the problem is elsewhere. I see that your snippet starts with a commented out print
statement printing the return value of a call to rtuniform
, and this matches what you posted of your interactive session. My guess is that you were executing an older version of the function that just printed and exited immediatly (implicitely returning None
), then you edited your code but failed to properly reload your function.
编辑:从你发布的代码片段来看,runloopg确实返回了一个列表,所以问题在别处。我看到您的代码片段从一个注释掉的print语句开始,打印一个调用rtuniform的返回值,这与您在交互式会话中发布的内容相匹配。我的猜测是,您正在执行一个旧版本的函数,它只是立即打印和退出(隐式返回None),然后编辑您的代码,但是没有正确地重新加载您的函数。
#3
0
Since your method is returning a generator, you need to consume it:
由于您的方法是返回生成器,您需要使用它:
for i in runloopg(10,0.1,6):
print(i)
p = list(runloopg(10,0.1,6))
# If you just want the first item:
first_item = next(runloopg(10,0.1,6))
#4
0
It looks like your runloopg
function is missing a return
. So, it does something like this:
看起来你的runloopg函数没有返回。它是这样的:
def runloopg():
[(0,0,0,0,0,1), (0.01,0,0,0,0,1), (0.0062349,0.00781831,0,0,0,1)] # and the rest
or this:
或:
def runloop():
x = [(0,0,0,0,0,1), (0.01,0,0,0,0,1), (0.0062349,0.00781831,0,0,0,1)] # and the rest
instead of this:
而不是:
def runloopg():
return [(0,0,0,0,0,1), (0.01,0,0,0,0,1), (0.0062349,0.00781831,0,0,0,1)] # and the rest
In the first version, that line is run, its result is then immediately discarded, and the function continues. In the second, the result is stored in a variable, and the function continues. In both cases, the function then falls off the end - and Python will return None
for you when that happens. In the last version, the result you've calculated is returned, and ends up being assigned to p
- so p
will be a list, and p[0]
will work.
在第一个版本中,该行运行,其结果随即被丢弃,函数继续。在第二种情况下,结果存储在一个变量中,函数继续。在这两种情况下,函数都会掉到最后,而当发生这种情况时,Python会为您返回None。在最后一个版本中,您计算的结果被返回,最后被分配到p - so p将是一个列表,而p[0]将会起作用。
#1
-2
A generator object is returned. Once you used a generator, it's exhausted and you can't use it again.
返回一个生成器对象。一旦你使用了发电机,它就耗尽了,你不能再用它了。
But you can take all of it's values as a list:
但是你可以把它的所有值作为一个列表:
p = list(runloopg(10,0.1,6))
#2
1
You didn't post the relevant code - your function's definition - but very obviously this function returns None
.
你没有发布相关的代码——你的函数的定义——但是很明显这个函数没有返回。
edit: Ok from the snippet you posted runloopg
does indeed return a list so the problem is elsewhere. I see that your snippet starts with a commented out print
statement printing the return value of a call to rtuniform
, and this matches what you posted of your interactive session. My guess is that you were executing an older version of the function that just printed and exited immediatly (implicitely returning None
), then you edited your code but failed to properly reload your function.
编辑:从你发布的代码片段来看,runloopg确实返回了一个列表,所以问题在别处。我看到您的代码片段从一个注释掉的print语句开始,打印一个调用rtuniform的返回值,这与您在交互式会话中发布的内容相匹配。我的猜测是,您正在执行一个旧版本的函数,它只是立即打印和退出(隐式返回None),然后编辑您的代码,但是没有正确地重新加载您的函数。
#3
0
Since your method is returning a generator, you need to consume it:
由于您的方法是返回生成器,您需要使用它:
for i in runloopg(10,0.1,6):
print(i)
p = list(runloopg(10,0.1,6))
# If you just want the first item:
first_item = next(runloopg(10,0.1,6))
#4
0
It looks like your runloopg
function is missing a return
. So, it does something like this:
看起来你的runloopg函数没有返回。它是这样的:
def runloopg():
[(0,0,0,0,0,1), (0.01,0,0,0,0,1), (0.0062349,0.00781831,0,0,0,1)] # and the rest
or this:
或:
def runloop():
x = [(0,0,0,0,0,1), (0.01,0,0,0,0,1), (0.0062349,0.00781831,0,0,0,1)] # and the rest
instead of this:
而不是:
def runloopg():
return [(0,0,0,0,0,1), (0.01,0,0,0,0,1), (0.0062349,0.00781831,0,0,0,1)] # and the rest
In the first version, that line is run, its result is then immediately discarded, and the function continues. In the second, the result is stored in a variable, and the function continues. In both cases, the function then falls off the end - and Python will return None
for you when that happens. In the last version, the result you've calculated is returned, and ends up being assigned to p
- so p
will be a list, and p[0]
will work.
在第一个版本中,该行运行,其结果随即被丢弃,函数继续。在第二种情况下,结果存储在一个变量中,函数继续。在这两种情况下,函数都会掉到最后,而当发生这种情况时,Python会为您返回None。在最后一个版本中,您计算的结果被返回,最后被分配到p - so p将是一个列表,而p[0]将会起作用。