python在函数中使用yield from [英] python using yield from in a function
本文介绍了python在函数中使用yield from的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类似的列表:
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 from并同时修改迭代中的项目.也许我必须在函数中使用 *args 和/或 **kwargs,但不确定我有以及如何使用.
works here but it is to understand how to use yield from in a function and modifying items in the iterable at the same time. Maybe i have to use *args and/or **kwargs in the function but not sure i have and how to.
推荐答案
yield from
是一个传递;您正在委托给可迭代对象.将可迭代对象包装在生成器表达式中以转换元素,或者不使用 yield from
而是使用显式循环:
yield from
is a pass-through; you are delegating to the iterable. Either wrap the iterable in a generator expression to transform elements, or don't use yield from
but an explicit loop:
def f(iterable):
yield from (i.replace(',', '') for i in iterable)
或
def f(iterable):
for item in iterable:
yield item.replace(',', '')
这篇关于python在函数中使用yield from的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文