[英]python using yield from in a function
我有一個列表,如:
list=['2,130.00','2,140.00','2,150.00','2,160.00']
我想使用像這樣的功能
def f(iterable):
yield from iterable
並申請
float(item.replace(',','')) for item in iterable
同時這樣
f(list)
回報
[2130.00,2140.00,2150.00,2160.00]
我知道
[float(x.replace(',','')) for x in list]
在這里工作,但它是要了解如何在函數中使用yield和同時修改iterable中的項目。 也許我必須在函數中使用* args和/或** kwargs但不確定我有和如何。
yield from
是一種傳遞; 你委托給iterable。 將iterable包裝在生成器表達式中以轉換元素,或者不使用yield from
而是使用顯式循環:
def f(iterable):
yield from (i.replace(',', '') for i in iterable)
要么
def f(iterable):
for item in iterable:
yield item.replace(',', '')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.