如何将生成器的yield语句传递给另一个函数。 -Python
我一直在这个网站上阅读,似乎找不到我想要的具体答案。我尝试阅读 david beasly 关于迭代和生成器的幻灯片,但仍然无法完全得到我正在寻找的答案,尽管问题看起来很简单。我正在运行基于时钟的模拟(Brian 用于神经网络),并且我有一个生成器,它可以操纵输出并将它们添加到运行总和中(以便简单的低通滤波器有 n 指数衰减) 。然后我想在每个时间步获取这些生成器的输出,然后在另一个函数中使用它们来更新一些状态变量,它说由于该项目是生成器类型,所以我不能这样做。代码和代码解释如下:
import numpy
our_range=numpy.arange(0*ms,duration+defaultclock.dt,defaultclock.dt)
a=our_range
c=defaultclock.t #this is a clock that is part of the program i'm running, it updates every #timestep and runs through the range given above
def sum_tau(neuron): #this gives a running sum which i want to access (the alphas can be ignored as they are problem specific)
for c in a: #had to express it like this (with c and a) or it wouldn't run
if c ==0:
x=0
elif defaultclock.still_running()==False:
return
else:
x = x*(1-alpha) + p(neuron)*alpha
print x
yield x
#p(neuron) just takes some of the neurons variables and gives a number
b=sum_tau(DN) #this is just to specify the neuron we're working on, problem specific
@network_operation
def x():
b.next()
@network_operation意味着每个时钟时间步将执行下面的函数,因此将总和更新为其所需的值。 现在我想要做的是通过键入以下内容来更新用于模拟的值(其中 d 是另一个生成器的输出,未显示,但与 b 非常相似):
ron= (d/(1-b))
但是,它说我不能在中使用生成器对象这样,我使用 print 语句来确定 b (和 d) 在每个时间步(运行模拟时)给出我想要的输出,但我似乎无法获取这些输出并用它们做任何事情。 (更具体地说,int 和生成器不支持操作数类型“-”。我尝试使用 float() 将其转换为数字,但显然这不起作用,因为同样的原因,我觉得必须有一个非常简单的解决方案来解决我的问题,但是我似乎找不到它。 提前致谢。
i've been reading on this site and can't seem to find the specific answer i want. i've tried reading david beasly's slides on iteration and generators but still can't quite get the answer i'm looking for though the question seems simple. i'm running a clock-based simulation (Brian for neural networking) and i have a generator that is manipulating the outputs and adding them to a running sum (in order for there to be n exponential decay for a simple low-pass filter). i then want to take the outputs of these generators, at each time-step and then use them in another function to update some of the state variables, it says that since the item is of the generator type i cannot do this. code and explanation of code is as follows:
import numpy
our_range=numpy.arange(0*ms,duration+defaultclock.dt,defaultclock.dt)
a=our_range
c=defaultclock.t #this is a clock that is part of the program i'm running, it updates every #timestep and runs through the range given above
def sum_tau(neuron): #this gives a running sum which i want to access (the alphas can be ignored as they are problem specific)
for c in a: #had to express it like this (with c and a) or it wouldn't run
if c ==0:
x=0
elif defaultclock.still_running()==False:
return
else:
x = x*(1-alpha) + p(neuron)*alpha
print x
yield x
#p(neuron) just takes some of the neurons variables and gives a number
b=sum_tau(DN) #this is just to specify the neuron we're working on, problem specific
@network_operation
def x():
b.next()
the @network_operation means that every clock timestep the function below will be executed, therefore updating the sum to it's required value.
Now what i want to do here is update a value that is used for the simulation (where d is the output to another generator, not shown, but very similar to b) by typing:
ron= (d/(1-b))
However, it says i cannot use a generator object in this way, i have used print statements to determine that that b (and d) give the outputs i want every timestep (when the simulation is run) but I cannot seem to take these outputs and do anything with them. (more specifically unsupported operand type '-' for int and generator. i tried converting it to a number with float() but obviously this doesn't work for the same reason, i feel there must be a very simple solution to my problem but i can't seem to find it.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“更具体地说,int 和生成器不支持操作数类型 '-'” 接受提示。
您不能在简单的公式中使用生成器。您必须使用生成器表达式“扩展”它。
有一个发电机
b
,对吗?b
不是一个“值”。这是某种值序列。因此,您必须将序列中的每个值应用到您的公式中。
将获取序列的每个值并计算一个新值。
(尚不清楚这是否真的有用,因为当
b
是值的集合时,原始的 ron= (d/(1-b)) 并没有多大用处感觉。)"more specifically unsupported operand type '-' for int and generator" Take the hint.
You can't use a generator in a trivial formula. You have to "expand" it with a generator expression.
Has a generator
b
, right?b
is not a "value". It's some kind of sequence of values.So, you have to apply each value in the sequence to your formula.
will get each value of the sequence and compute a new value.
(It's not clear if this is actually useful, since the original
ron= (d/(1-b))
whenb
is a collection of values doesn't make much sense.)