3 ms·
I'm not a python programmer but I find the last 4 lines rather hard to read. Unless there is a performance reason to not do so, I'd argue that you are iterating
by alkonaut 8y ago
I'm not a python programmer but I find the last 4 lines rather hard to read. Unless there is a performance reason to not do so, I'd argue that you are iterating over pairs (i,j) here and I'd make the code clear. There are 100 pairs, and once you find one that satisfies the condition you are done. Again, not a python programmer but it might look like
(x, y) for x in range(10) for y in range(10)
if condition(i,j)
dosomething;
break;
Edit, ("zip" is not the outer product... )
- quietbritishjim 8y agoThe relevant function is itertools.product() [1]. It can give the Cartesian product of arbitrary iterables, but if you have the same iterable more than once, as in this example, you can take advantage of the repeat= optional parameter to simplify the call a little bit: for x, y in itertools.product(range(10), repeat=2): if condition(x, y): dosomething(x, y) break Alternatively, you can use Python list comprehensions: prodlist = [x, y for x in range(10) for y in range(10)] for x, y in prodlist: if condition(x, y): dosomething(x, y) break Overall, I still prefer refactoring into a function and using return, as the GP comment suggested. [1] https://docs.python.org/3/library/itertools.html#itertools.product https://docs.python.org/3/library/itertools.html#itertools.p...