Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“为来自”的对象应用函数;收益来自;用python_Python_Python 3.x_Syntax_Yield_Yield From - Fatal编程技术网

“为来自”的对象应用函数;收益来自;用python

“为来自”的对象应用函数;收益来自;用python,python,python-3.x,syntax,yield,yield-from,Python,Python 3.x,Syntax,Yield,Yield From,我想知道是否有一种方法可以在语法上把它压缩成一行 # currently I have def some_func(): for match in re.finditer(regex, string): yield other_func(match) 你可以用。map接受两个参数:函数和iterable。它迭代iterable并应用函数并返回一个迭代器(它生成映射值-函数(第一项)、函数(seoncd项),…) 这里的yield from是不必要的,因为map返回一个

我想知道是否有一种方法可以在语法上把它压缩成一行

# currently I have

def some_func():
    for match in re.finditer(regex, string):
        yield other_func(match)
你可以用。
map
接受两个参数:函数和iterable。它迭代iterable并应用函数并返回一个迭代器(它生成映射值-函数(第一项)、函数(seoncd项),…)

这里的yield from
是不必要的,因为
map
返回一个迭代器(在Python 3.x中):


例如:

def some_func():
    return map(other_func, re.finditer(regex, string))
你可以用。
map
接受两个参数:函数和iterable。它迭代iterable并应用函数并返回一个迭代器(它生成映射值-函数(第一项)、函数(seoncd项),…)

这里的yield from
是不必要的,因为
map
返回一个迭代器(在Python 3.x中):


例如:

def some_func():
    return map(other_func, re.finditer(regex, string))

对于简单和简短的内容,您可以返回一个生成器表达式,该表达式基本上与yield相同

>>> import re
>>>
>>> def other_func(match):
...     return match.group()
...
>>> def some_func():
...     return map(other_func, re.finditer(regex, string))
...
>>> regex = '.'
>>> string = 'abc'
>>> list(some_func())
['a', 'b', 'c']

对于简单和简短的内容,您可以返回一个生成器表达式,该表达式基本上与yield相同

>>> import re
>>>
>>> def other_func(match):
...     return match.group()
...
>>> def some_func():
...     return map(other_func, re.finditer(regex, string))
...
>>> regex = '.'
>>> string = 'abc'
>>> list(some_func())
['a', 'b', 'c']
def some_func():
    return (other_func(match) for match in re.finditer(regex, string))