腾讯云
开发者社区
文档
建议反馈
控制台
登录/注册
首页
学习
活动
专区
圈层
工具
MCP广场
文章/答案/技术大牛
搜索
搜索
关闭
发布
文章
问答
(9999+)
视频
沙龙
2
回答
为什么
要让
StopIteration
输出
while
循环
python
、
python-3.x
、
generator
我正在使用yield from,但我不知道
while
对yield的影响。如果我将yield from放在一个
while
循环
中,它工作得很好,但是当我同时取消这个
循环
时,一个异常就会发生。final_result = {} total = 0
while
True: total += x return
浏览 16
提问于2019-06-23
得票数 1
回答已采纳
18
回答
如何模拟do-
while
循环
?
python
、
while-loop
、
do-while
我需要在Python程序中模拟do-
while
循环
。不幸的是,以下简单的代码不起作用:iterator = list_of_ints.() break 它
输出
以下
输出
,而不是"1,2,3,done“:[stdout:]2'
浏览 6
提问于2009-04-13
得票数 967
回答已采纳
2
回答
更改body以生成iterable的所有值
python
、
iterator
、
generator
、
yield
、
iterable
def alternate(*args): try: yield next(a) pass 目前,我的
输出
是'afhbgic我怎样才能改变我的代码,让它打印正确的
输出
?
浏览 2
提问于2020-11-05
得票数 1
1
回答
为什么
next会引发一个“
StopIteration
”,而“for”会返回一个正常的返回值?
python
、
iterator
、
generator
、
stopiteration
在这段代码中,
为什么
使用for不会产生
StopIteration
,或者for
循环
捕获所有异常,然后静默退出?在这种情况下,
为什么
我们会有无关的return?或者是return None引起的raise
StopIteration
?def countdown(n):
while
n >= 9: yield ndef
浏览 5
提问于2013-01-19
得票数 82
回答已采纳
1
回答
使
循环
在后台工作
python
、
for-loop
、
while-loop
所以我在这里搞不懂 self.time += 1for item我希望for loop和
while
loop的工作时间相同。而self.item += 1,那个for item...也在运行
浏览 2
提问于2022-03-04
得票数 2
回答已采纳
2
回答
同时为
循环
编写python?
python
、
control-flow
为了教育目的,我想知道是否有可能在python中重写所有for
循环
,就像
while
循环
一样。理论上,如果和同时是流控制的最小集合。搜索结果显示了基于范围的
循环
的问题,这显然是可行的,因为开始和结束的索引都是公开的。其他呢?例如,
循环
遍历各种形式的列表如何?
浏览 2
提问于2016-01-24
得票数 2
回答已采纳
5
回答
产量如何捕获
StopIteration
异常?
python
、
yield
、
stopiteration
为什么
在示例函数中终止:
while
True: yield valgen = func(iterable)def func2(iterable): val = next(iterable)
浏览 4
提问于2013-05-09
得票数 44
回答已采纳
2
回答
作为布尔语句的迭代器?
python
、
python-2.7
、
python-3.x
我偶然发现了这样的代码: iters = map(iter, args) res = [next(i) fori in iters]我不确定: 正如我所尝试的,
while
iters是如何工作的:代码的作者还说,因为map在3.X中返回一个“一次迭代”,并且“一旦我们在
循环
中运行了一次列表理解,iter
浏览 4
提问于2017-08-19
得票数 6
回答已采纳
3
回答
当
循环
被卡住
python
、
python-3.x
我被告知要将“For”
循环
转换为“
while
”:total = 0 ifvalue is None: total += value这是
while
循环
total = 0
while
i < len(sequence):
浏览 2
提问于2022-02-27
得票数 0
1
回答
在生成器上
循环
python
、
for-loop
、
generator
、
python-3.7
我想
循环
遍历一个生成器,并产生生成器的
输出
,直到
StopIteration
。
while
funct_one(list, number).__next__() !=
StopIteration
:... break... 246 File "<stdin>", line
浏览 0
提问于2018-10-26
得票数 0
2
回答
ValueError在
StopIteration
之前使用map(下一步,可迭代)
python
、
python-3.x
、
iterator
、
map-function
我在4个列表迭代器上写了一个
循环
,如下所示:it1 = iter(['bar','foo']) a, b, c, d = map(next, (it0, it1, it2, it3))
浏览 0
提问于2016-04-27
得票数 0
回答已采纳
3
回答
python中生成器函数中引发的
StopIteration
异常处理
python
、
python-3.x
、
iterator
、
generator
、
yield
下面是我的生成器函数代码: n1=0
while
True: raise
StopIteration
我对版本1的预期
输出
:125
while
True: print(next(seq))
输出
:125 RuntimeError: generato
浏览 3
提问于2021-09-02
得票数 1
回答已采纳
1
回答
已耗尽迭代器,但For
循环
中没有错误
python
、
python-3.x
、
iterator
(在Python 3.2中)for i in miters:15 -->
StopIteration
为什么
我不能在第二个for
循环
中得到'
StopIteration
‘错误信息?‘'miter’已经用完了,所以如果我再次
循环
它,我想会有一个'
StopIteration
'... 有人能告诉我
为什么
吗?
浏览 0
提问于2013-02-21
得票数 1
2
回答
在python中,如何检查标准输入流(sys.stdin)的末尾,并对其进行特殊处理。
python
、
stdin
、
eof
sys.stdin: if is **END OF StdIn**:经过几次尝试,我现在要做的是: try: print line,或者这样做: line = sys.stdin.readline() if not lin
浏览 3
提问于2014-06-06
得票数 17
回答已采纳
3
回答
如何在Python中显式地检测生成器端?
python
、
generator
np.reshape(data, (-1, 2)) yield row[0], row[1]
while
浏览 2
提问于2017-09-12
得票数 5
回答已采纳
1
回答
有没有内置的方式来检查#next或#peek是否会引发
StopIteration
?
ruby
、
enumerator
这方面的问题是,enum可能在最后,并且有几个值传递给enum,因此我遇到了enum.peek引发
StopIteration
的问题,因为enum已经完成。有没有办法在我调用enum.peek或enum.next之前,设置一个守卫来检查它是否会导致
StopIteration
。比如会有这种行为的东西?begin rescue
StopIteration
endend
浏览 0
提问于2019-09-16
得票数 3
回答已采纳
2
回答
退出Kernel#loop
ruby
在中,有一个使用break来跳出
循环
的例子。否则,文档将讨论如何引发
StopIteration
错误。我两个都试过了:loop do i+=1end loop do i+=1end
输出
是相同的。我认为应该有,否则
为什么
要费心定义一个error类和随之而来的所有管理呢?
浏览 2
提问于2015-01-05
得票数 2
2
回答
防止异常终止Python迭代器
python
yield 4 # this never happens
while
1: value = my_iter.next()print 'value:',value break print 'An error occurred: %s' % (e,)value: 1 value
浏览 0
提问于2013-04-27
得票数 4
回答已采纳
1
回答
循环
相对于时间和下一个性能
python
、
performance
、
iteration
、
python-internals
在某些情况下,在生成器中
循环
使用
while
和next (与try/except
StopIteration
一起使用)似乎比使用更简单的for
循环
更自然。然而,这是一个巨大的性能成本。µs ± 8.78 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each) _gen = gen()
while
True:except
StopIteration<
浏览 1
提问于2019-09-24
得票数 3
回答已采纳
1
回答
在回路体中提出“停止”
python
、
loops
我一直认为,一个精疲力竭的迭代器会引发
StopIteration
,而
循环
会捕捉到这一点。这似乎与中所说的一致。现在,运行任何itr = iter(a) next(itr) next(itr) itr = iter这似乎与上述链接中的内容不一致: 在
while
循环
的主体中引发的
StopIteration
异常将终止(并被捕获)
while
-
循环
,从而导致
浏览 10
提问于2021-12-08
得票数 0
回答已采纳
点击加载更多
热门
标签
更多标签
云服务器
ICP备案
对象存储
云点播
实时音视频
活动推荐
运营活动
广告
关闭
领券