Probabilmente non è quello che si sta sperando per, ma di solito si vorrebbe avere un break
dopo aver impostato find
a True
for word1 in buf1:
find = False
for word2 in buf2:
...
if res == res1:
print "BINGO " + word1 + ":" + word2
find = True
break # <-- break here too
if find:
break
un altro modo è quello di utilizzare un generatore di espressione di schiacciare il for
in un singolo ciclo
for word1, word2 in ((w1, w2) for w1 in buf1 for w2 in buf2):
...
if res == res1:
print "BINGO " + word1 + ":" + word2
break
Si può anche considerare l'utilizzo di itertools.product
from itertools import product
for word1, word2 in product(buf1, buf2):
...
if res == res1:
print "BINGO " + word1 + ":" + word2
break
fonte
2010-04-08 03:13:41
+1 semplice ma elegante! –
itertools.product() è un ottimo approccio. –
Oggi ho imparato qualcosa :) – wonzbak