Using yield and zip on python. Can anyone help me understand this piece of code?

norman123123

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)
tiwo

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

  • ask the manager to sack the person who wrote this code :-)
  • if there's meaning to it, please let me know

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Can anyone help me understand this piece of code?

From Dev

Can anyone help me to understand typedef in this program?

From Dev

Need Help to understand the piece of code

From Dev

Can anyone help me with this python program?

From Dev

Can someone help me to understand output of this code

From Dev

I dont't understand why im getting syntax error on my code below. Can anyone help me?

From Dev

Can someone help me understand this for loop in python

From Dev

Can anyone help me out to try pagination for the below code...!

From Dev

Whats wrong with this php code?Can anyone help me

From Dev

Can anyone help me to find what is wrong in below code?

From Dev

Error Code 13 in Mysql on UNIX. Can anyone Help me?

From Dev

Can anyone help me out to try pagination for the below code...!

From Dev

Can anyone help me with these minidumps?

From Dev

Can anyone help me with this javascript?

From Dev

Can anyone help me on using GUI page in intelliJ

From Dev

Can anyone help me with this TypeError I keep getting in Python 2.7?

From Dev

Can anyone help me what is wrong with my python script?

From Dev

Please help me with the following piece of code?(JAVA)

From Dev

Please help me with the following piece of code?(JAVA)

From Java

Can anyone help me understand why im getting this runtime error with my leetcode problem? Longest Prefix

From Dev

can someone help me understand why VS Code is throwing this error?

From Dev

can someone help me understand why VS Code is throwing this error?

From Dev

Can anyone help me with Linq Sorting

From Dev

Can anyone help me on this Link issue:

From Dev

Can anyone help me to optimize my query?

From Dev

Can anyone help me to find a error in PHP?

From Dev

Can anyone help me to optimize my query?

From Dev

Can anyone help me to make require working?

From Dev

Can anyone help me with this simple For Loop?

Related Related

  1. 1

    Can anyone help me understand this piece of code?

  2. 2

    Can anyone help me to understand typedef in this program?

  3. 3

    Need Help to understand the piece of code

  4. 4

    Can anyone help me with this python program?

  5. 5

    Can someone help me to understand output of this code

  6. 6

    I dont't understand why im getting syntax error on my code below. Can anyone help me?

  7. 7

    Can someone help me understand this for loop in python

  8. 8

    Can anyone help me out to try pagination for the below code...!

  9. 9

    Whats wrong with this php code?Can anyone help me

  10. 10

    Can anyone help me to find what is wrong in below code?

  11. 11

    Error Code 13 in Mysql on UNIX. Can anyone Help me?

  12. 12

    Can anyone help me out to try pagination for the below code...!

  13. 13

    Can anyone help me with these minidumps?

  14. 14

    Can anyone help me with this javascript?

  15. 15

    Can anyone help me on using GUI page in intelliJ

  16. 16

    Can anyone help me with this TypeError I keep getting in Python 2.7?

  17. 17

    Can anyone help me what is wrong with my python script?

  18. 18

    Please help me with the following piece of code?(JAVA)

  19. 19

    Please help me with the following piece of code?(JAVA)

  20. 20

    Can anyone help me understand why im getting this runtime error with my leetcode problem? Longest Prefix

  21. 21

    can someone help me understand why VS Code is throwing this error?

  22. 22

    can someone help me understand why VS Code is throwing this error?

  23. 23

    Can anyone help me with Linq Sorting

  24. 24

    Can anyone help me on this Link issue:

  25. 25

    Can anyone help me to optimize my query?

  26. 26

    Can anyone help me to find a error in PHP?

  27. 27

    Can anyone help me to optimize my query?

  28. 28

    Can anyone help me to make require working?

  29. 29

    Can anyone help me with this simple For Loop?

HotTag

Archive