i have a list like:
list=['2,130.00','2,140.00','2,150.00','2,160.00']
i would like to use a function like
def f(iterable):
yield from iterable
and applying
float(item.replace(',','')) for item in iterable
at the same time so that
f(list)
returns
[2130.00,2140.00,2150.00,2160.00]
I know
[float(x.replace(',','')) for x in list]
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
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)
or
def f(iterable):
for item in iterable:
yield item.replace(',', '')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.