我在Python 3.2中有一个代码,我想在Python 2.7中运行它.我确实转换了它(已经把missing_elements两个版本的代码都放了)但我不确定这是否是最有效的方法.基本上如果yield from在missing_element功能的上半部分和下半部分有两个如下所示的调用会发生什么?两个部分(上部和下部)中的条目是否在一个列表中相互附加,以便父级递归函数与yield from调用一起使用并将两个部分一起使用?
def missing_elements(L, start, end): # Python 3.2
if end - start <= 1:
if L[end] - L[start] > 1:
yield from range(L[start] + 1, L[end])
return
index = start + (end - start) // 2
# is the lower half consecutive?
consecutive_low = L[index] == L[start] + (index - start)
if not consecutive_low:
yield from missing_elements(L, start, index)
# is the upper part consecutive?
consecutive_high = L[index] …Run Code Online (Sandbox Code Playgroud) 有没有办法混合递归和yield声明?例如,无限数字生成器(使用递归)将类似于:
def infinity(start):
yield start
# recursion here ...
>>> it = infinity(1)
>>> next(it)
1
>>> next(it)
2
Run Code Online (Sandbox Code Playgroud)
我试过了:
def infinity(start):
yield start
infinity(start + 1)
Run Code Online (Sandbox Code Playgroud)
和
def infinity(start):
yield start
yield infinity(start + 1)
Run Code Online (Sandbox Code Playgroud)
但他们没有做我想要的东西,第一个停止后,它会产生start和第二个产生start,那么发生器,然后停了下来.
注意:请知道您可以使用while循环执行此操作:
def infinity(start):
while True:
yield start
start += 1
Run Code Online (Sandbox Code Playgroud)
我只是想知道这是否可以递归完成.
有没有办法检测是否#content_for实际应用于yieldRails中的作用域?
一个典型的例子是:
<title><%= yield :page_title %></title>
Run Code Online (Sandbox Code Playgroud)
如果模板没有设置
<% content_for :page_title, "Something here" %>
Run Code Online (Sandbox Code Playgroud)
有没有办法让布局把其他东西放在那里?
我尝试#content_for在布局本身中定义它,但这只会导致文本加倍.我也尝试过:
<%= (yield :page_title) or default_page_title %>
Run Code Online (Sandbox Code Playgroud)
#default_page_title视图帮助器在哪里.
这只是让街区完全空了.
当在Python中的同一函数中使用yield和return时,究竟会发生什么?
def find_all(a_str, sub):
start = 0
while True:
start = a_str.find(sub, start)
if start == -1: return
yield start
start += len(sub) # use start += 1 to find overlapping matches
Run Code Online (Sandbox Code Playgroud)
它还是发电机吗?
考虑以下代码:
def mygen():
yield (yield 1)
a = mygen()
print(next(a))
print(next(a))
Run Code Online (Sandbox Code Playgroud)
输出结果:
1
None
Run Code Online (Sandbox Code Playgroud)
口译员到底在“外部”做什么?
我知道yield将函数转换为生成器,但yield表达式本身的返回值是多少?例如:
def whizbang():
for i in range(10):
x = yield i
Run Code Online (Sandbox Code Playgroud)
x这个函数执行时变量的值是多少?
我已经阅读了Python文档:http://docs.python.org/reference/simple_stmts.html#grammar-token-yield_stmt,似乎没有提到yield表达式本身的值.
我在Nodejs v0.11.2中使用了生成器,我想知道如何检查我的函数的参数是生成器函数.
我找到了这种方式,typeof f === 'function' && Object.getPrototypeOf(f) !== Object.getPrototypeOf(Function)但我不确定这是否好(并在将来工作)方式.
你对这个问题有什么看法?
我正在尝试对给定路径下的所有文件执行某些操作.我不想事先收集所有的文件名然后用它们做一些事情,所以我尝试了这个:
import os
import stat
def explore(p):
s = ''
list = os.listdir(p)
for a in list:
path = p + '/' + a
stat_info = os.lstat(path )
if stat.S_ISDIR(stat_info.st_mode):
explore(path)
else:
yield path
if __name__ == "__main__":
for x in explore('.'):
print '-->', x
Run Code Online (Sandbox Code Playgroud)
但是这个代码在命中它们时会跳过目录,而不是让它们产生内容.我究竟做错了什么?
根据这个问题的答案,C#中的yield break相当于python中的return.在正常情况下,'return'确实会停止发电机.但是如果你的函数除了返回之外什么都不做,你将获得一个None而不是一个空的迭代器,它在C#中由yield break返回
def generate_nothing():
return
for i in generate_nothing():
print i
Run Code Online (Sandbox Code Playgroud)
你会得到一个TypeError:'NoneType'对象是不可迭代的.但是如果我在返回之前添加一个从不运行的yield,这个函数会返回我期望的结果.
def generate_nothing():
if False: yield None
return
Run Code Online (Sandbox Code Playgroud)
如果有效,但似乎有线.谁有更好的主意?
谢谢,
我想要一个函数,is_just_started其行为类似于以下内容:
>>> def gen(): yield 0; yield 1
>>> a = gen()
>>> is_just_started(a)
True
>>> next(a)
0
>>> is_just_started(a)
False
>>> next(a)
1
>>> is_just_started(a)
False
>>> next(a)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration
>>> is_just_started(a)
False
Run Code Online (Sandbox Code Playgroud)
我该如何实现这个功能?
我查看了该.gi_running属性,但它似乎用于其他东西.
如果我知道需要发送到生成器的第一个值,我可以这样做:
def safe_send(gen, a):
try:
return gen.send(a)
except TypeError as e:
if "just-started" in e.args[0]:
gen.send(None)
return gen.send(a)
else:
raise
Run Code Online (Sandbox Code Playgroud)
然而,这似乎令人憎恶.
yield ×10
python ×8
generator ×5
recursion ×2
ecmascript-6 ×1
iterator ×1
javascript ×1
python-2.7 ×1
python-2.x ×1
python-3.x ×1
ruby ×1
yield-from ×1