关于循环的问题 [英] question about for cycle
问题描述
大家好,
我有以下代码:
我在generator_a中的
:#the first" for"循环
for generator in generator_b:
if something_happen:
#在这里做点什么...,我希望外循环打破
休息
如果我想要外部的for,我该怎么办?周期继续还是休息?如果我
将继续或打破在内循环中,它对外部
周期没有影响。
我还有另外一个问题。检查列表中是否有重复项目的最有效方法是哪种?
?列表中的项目可能无法进行哈希处理,因此
set()可能无法在列表中使用。
问候,
Hi all,
I have the following code:
for i in generator_a: # the first "for" cycle
for j in generator_b:
if something_happen:
# do something here ..., I want the outer cycle to break
break
What should I do if I want the outer "for" cycle to continue or break ? If I
put a "continue" or "break" in the inner cycle, it has no effect on the outer
cycle.
And I have another question. Which is the most efficient way to check if there
are duplicate items in a list ? The items in the list may cannot be hashed, so
set() may not work on the list.
Regards,
推荐答案
9月29日上午11点04分,fdu.xia ... @ gmail.com < fdu.xia ... @ gmail.com>
写道:
....
On Sep 29, 11:04 am, "fdu.xia...@gmail.com" <fdu.xia...@gmail.com>
wrote:
....
如果我想要外部的for,我该怎么办?周期继续还是休息?如果我
将继续或打破在内循环中,它对外部
周期没有影响。
What should I do if I want the outer "for" cycle to continue or break ? If I
put a "continue" or "break" in the inner cycle, it has no effect on the outer
cycle.
我也会对这个问题的惯用解决方案感兴趣。我可以
从丑陋中看到一些解决方案:
我在范围内的
(10):
do_break =真的
为j在范围内(10):
如果j == 6:
break
else:
do_break = False
如果do_break:
休息
这将打破外环如果内环退出则休息。
使用例外:
for i in range(10):
尝试:
范围内的j(10):
打印i,j
如果j == 6:
提高MyException
除了MyException,e:
休息#或继续等等。
封装在一个函数中并使用return:
def get_value():
for i in range(10):
for j范围(10):
打印i,j
如果j == 6:
返回fn(i,j)
我想在某种程度上它取决于确切的情况
其中哪些更隋表。还有其他推荐的
解决方案吗?
-
Ant ...
>
Ant写道:
Ant wrote:
9月29日上午11点04分,fdu.xia ... @ gmail .COM" < fdu.xia ... @ gmail.com>
写道:
...
On Sep 29, 11:04 am, "fdu.xia...@gmail.com" <fdu.xia...@gmail.com>
wrote:
...
>如果我想要外部的for,我该怎么办?周期继续还是休息?如果我把继续或打破在内循环中,它对外循环没有影响。
>What should I do if I want the outer "for" cycle to continue or break ? If I
put a "continue" or "break" in the inner cycle, it has no effect on the outer
cycle.
我也会对这个问题的惯用解决方案感兴趣。我可以
从丑陋中看到一些解决方案:
我在范围内的
(10):
do_break =真的
为j在范围内(10):
如果j == 6:
break
else:
do_break = False
如果do_break:
中断
I''d also be interested in the idiomatic solution to this one. I can
see a number of solutions, from the ugly:
for i in range(10):
do_break = True
for j in range(10):
if j == 6:
break
else:
do_break = False
if do_break:
break
这是一个不需要标志的变体
Here''s a variant that doesn''t need the flag
>> inner =" abc"
outer =" xbz"
for i in outer:
>>inner = "abc"
outer = "xbz"
for i in outer:
。 ...对于内在的k:
....如果我= = k:
....打印找到,我是
....休息
....其他:
....打印我,找不到
....继续
....休息
。 ...
x未找到
找到b
但我通常更喜欢这样的辅助函数
.... for k in inner:
.... if i == k:
.... print "found", i
.... break
.... else:
.... print i, "not found"
.... continue
.... break
....
x not found
found b
but I usually prefer a helper function like this
def get_value():
for i in range(10):
for j in range(10):
打印i,j
如果j == 6:
返回fn(i,j)
def get_value():
for i in range(10):
for j in range(10):
print i, j
if j == 6:
return fn(i, j)
或者这个:
or this:
>> def f(i,inner) :
>>def f(i, inner):
....对于内在的k:
....如果我== k:
.... print" found,i
....返回True
....
.... for k in inner:
.... if i == k:
.... print "found", i
.... return True
....
>> for i in outer:
>>for i in outer:
....如果f(i,inner):
.... break
....打印我,找不到
....
x not found <找到b
b
彼得
.... if f(i, inner):
.... break
.... print i, "not found"
....
x not found
found b
Peter
Ant< an **** @ gmail.comwrote:
Ant <an****@gmail.comwrote:
9月29日上午11点04分,fdu.xia ... @ gmail.com < fdu.xia ... @ gmail.com>
写道:
...
On Sep 29, 11:04 am, "fdu.xia...@gmail.com" <fdu.xia...@gmail.com>
wrote:
...
>如果我想要外部的for,我该怎么办?循环继续或休息
?如果我把继续或打破在内循环中,它对外循环没有影响。
>What should I do if I want the outer "for" cycle to continue or break
? If I put a "continue" or "break" in the inner cycle, it has no
effect on the outer cycle.
....
....
我想在某种程度上它取决于关于
的确切情况哪一个更合适。还有其他推荐的
解决方案吗?
I guess to an extent it would depend on the exact situation as to
which of these is more suitable. Are there any other recommended
solutions to this?
我认为你错过的另一个Pythonic选项是转换嵌套的
通过编写生成器循环到单个循环中。
def范围(limit1,limit2):
range1,range2 = range( limit1),range(limit2)
for i in range1:
for j in range2:
yield i,j
....
for i,j in range(10,10):
......无论如何......
如果j == 6:
休息
I think the other Pythonic option you have missed is to convert the nested
for loops into a single loop by writing a generator.
def ranges(limit1, limit2):
range1, range2 = range(limit1), range(limit2)
for i in range1:
for j in range2:
yield i,j
....
for i, j in ranges(10, 10):
... whatever ...
if j==6:
break
这篇关于关于循环的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!