亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python 協程在產量上給出未知無

Python 協程在產量上給出未知無

長風秋雁 2022-09-06 10:22:00
我正在嘗試根據用戶輸入構建一些數學運算的管道,并嘗試打印該運算的累積結果。例如,輸入將是一個操作列表,然后是數字輸入的數量,然后是如下所示的數字:[square, accumulate]3123這應該返回如下內容:1514首先,它將 1 打印為 1*1,然后將該 1 與 2*2 給出 5 的結果相加,然后將其添加到 3*3 中,給出 14。但是我的方法有問題,第二個數字輸入總是變成None值,我不知道為什么。我被困在得到:1None10有什么想法嗎?這是我的代碼:import mathimport osimport randomimport reimport sysdef printer():    while True:        x = yield        print(x)def co_generator(n):    for _ in range(n):        x = int(input())        yield xdef get_root():    while True:        number = (yield)        yield math.floor(math.sqrt(number))def get_square():    number = 0    while True:        number = (yield)        yield number**2def accumulator():    acum = 0    while True:        acum += (yield)        yield acumdef operations_pipeline(numbers, operations, print_acum):    for num in numbers:        for i, w in enumerate(operations):            num = w.send(num)        print_acum.send(num)    for operation in operations:        operation.close()    print_acum.close()if __name__ == '__main__':    order = input().strip()    n = int(input())    numbers = co_generator(n)    print_acum = printer()    next(print_acum)    root = get_root()    next(root)    accumulate = accumulator()    next(accumulate)    square = get_square()    next(square)    operations_pipeline(numbers, eval(order), print_acum)
查看完整描述

3 回答

?
猛跑小豬

TA貢獻1858條經驗 獲得超8個贊

您正在編寫代碼,就好像接收值并發送值一樣。這不是它的工作方式。所有收益都發送一個值并接收一個值。(yield)yield whatever


當生成器執行時,它分兩個階段執行。首先,的參數成為當前或調用的返回值,生成器暫停執行。如果沒有參數,則使用。這就是你的s來自哪里yieldyieldnextsendNoneNone


其次,當執行另一個 或 時,生成器將取消暫停,并且參數(或者如果使用)將成為表達式的值。nextsendsendNonenextyield


您正在嘗試使用一個來接收參數,并使用另一個來設置 的返回值。你需要使用一個 single 來設置一個返回值并接收下一個 的參數。例如yieldsendsendyieldsendsend


def get_square():

    number = 0

    while True:

        number = yield number**2

或者,如果要使用單獨的 s 在生成器端發送和接收值,則需要使用單獨的(或)調用在另一端接收和發送值,并忽略 s。例如yieldsendnextNone


w.send(num)

num = next(w)

而不是 ,所以看起來像num = w.send(num)operations_pipeline


def operations_pipeline(numbers, operations, print_acum):

    for num in numbers:

        for w in operations:

            w.send(num)

            num = next(w)

        print_acum.send(num)

    for operation in operations:

        operation.close()

    print_acum.close()


查看完整回答
反對 回復 2022-09-06
?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

def rooter():

    number=0

    while True:

        number= yield math.floor(math.sqrt(number))

    


def squarer():

    number=0

    while True:

        number= yield number**2

產量將給沒有嘗試這種方式


def accumulator():

    number=0

    while True:

        number+= yield number 


查看完整回答
反對 回復 2022-09-06
?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

我相信你需要在這里的代碼中添加一個調用:nextoperations_pipeline


def operations_pipeline(numbers, operations, print_acum):

    for num in numbers:

        for i, w in enumerate(operations):

            num = w.send(num)

            next(w) # <<<--- this

沒有這個,我不相信它會回到第二次,假設你的第一個輸入是。get_square[square, accumulate]


查看完整回答
反對 回復 2022-09-06
  • 3 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號