I'm having trouble following what this piece of code does on a step by step level. All I know is that it prints the list [3.0, 4.0, 8.0] .
The first et variable has a yield(float) that doesnt do anything at first right? then with the next(m) it starts running the rest of the code and enters the While loop. Then on xtc, when it does zip(xt + [et], args) what's on [et] ?? is it empty? then how can you 'zip' if xt has 2 elements and args has 3?
def ar(*args):
et = yield(float)
xt = [1.0 for _ in range(len(args) - 1)]
while True:
xtc = sum(map(lambda x: x[0] * x[1], zip(xt + [et], args)))
xt = [xtc] + xt[:-1]
et = yield xtc
n = [1.0, 2.0, 3.0]
m = ar(-1.0, 1.0, 3.0)
next(m)
y = [m.send(i) for i in n]
print(y)
At next(m)
, the generator yields float
, which becomes the value of the expression next(m)
, but isn't used. Within ar
, however, we're not yet done with the first line
et = yield(float)
because we're still waiting for the value of yield(...)
, which comes from the first m.send(i)
(i is -1.0 at that point).
So [et]
equals [-1.0]
when it is first used in the loop.
I don't know if anything meaningful happens at all. I believe this to be a simplified, equivalent version:
def dot_product(u, v):
return sum(a*b for a, b in zip(u, v))
list_n = 1, 2, 3
list_of_args = -1, 1, 3
xt = [1] * (len(list_n) - 1)
for et in list_n:
xtc = dot_product(xt + [et], list_of_args)
xt.pop()
xt.insert(0, xtc)
print(xtc)
Please
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments