+ 3
How to exit from a double loop in a pythonic way?
yellow = ['255', '255', '0'] for row in range(50): for col in range(50): pixel = data[row][col] if pixel == yellow: upper_left = (row, col) break When pixel is yellow, how should be this loop terminated? Now it continues with the outer loop again which is not desired.
27 Antworten
+ 8
for row in range(50):
for col in range(50):
pixel = data[row][col]
if pixel == yellow:
upper_left = (row, col)
break
else:
continue
break
if second loop breaks then first loop breaks too
+ 4
The use of itertools.product or a function with return to terminate both loops that you already mentioned seem nice and concise, if that is what Pythonic means.
+ 4
On another note, would it be better (or more Pythonic) to use a tuple rather than a list to represent each colour, as the number of colour components is always constant(3) and doesn't grow?
+ 3
I would have also used a control flag(boolean variable) to terminate the outer loop but I guess it's not that Pythonic.
+ 3
guillem ardanuy
flag = False
for i in range(8):
for j in range(8):
if whatever:
flag = True
break
if flag:
break
This is what I called C-ish.
If I knew a shorter, *easily readable* technique, I would choose it.
Maybe it's also a bit a matter of getting used to the pattern
else
break
+ 2
Gabriel Ilie Another solution, seems to me more pythonic:
def find_yellow():
for raw in range(50):
for col in range(50):
if data[raw][col] == yellow:
return (raw, col)
upper_left = find_yellow()
+ 2
guillem ardanuy Many people use a boolean flag as control variable but the code is longer, eg:
found = False
while not found:
for row in range(50):
for col in range(50):
pixel = data[row][col]
if pixel == yellow:
upper_left = (row, col)
found = True
break
if found:
break
+ 2
guillem ardanuy Mirielle👽 For me, this 4-liner seems to be the most pythonic:
from itertools import product
for row, col in product(range(50), range(50)):
if data[row][col] == ['255', '255', '0']:
break
+ 2
Mirielle👽 Thanks for your comment and sorry for my unclear or uncomplete question.
The code actually works because the list is a matrix of strings, eg. [[[‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’]...]]. In this way, list items can be matched the variable yellow, eg:
data[25][25] # it is ['255', '255', '0']
yellow = ['255', '255', '0']
so data[25][25] == yellow is True
+ 2
Sonic “Pythonic” is somewhat subjective but there are guidelines, ie. The Zen of Python or PEP8.
“Exploiting the features of the Python language to produce code that is clear, concise and maintainable.
Pythonic means code that doesn't just get the syntax right but that follows the conventions of the Python community and uses the language in the way it is intended to be used.”
https://stackoverflow.com/questions/25011078/what-does-pythonic-mean
+ 2
HonFu
You are very advanced. Which solution would you prefer for exiting double loops?
+ 2
guillem ardanuy Just a quick tipp: you can try to use the @ sign before usernames.
+ 1
Mirielle👽 Thanks, Mirielle. How to break the first loop (row) when yellow is found?
+ 1
Mirielle👽 Actually, this list is a matrix of strings, eg. [[[‘0’, ‘0’, ‘0’], [‘0’, ‘0’, ‘0’]...]] which represents an 50x50 image’s RGB pixels
+ 1
Gabriel Ilie Yes, this works!
+ 1
🤣 double loop is double loop🧐🧐
You might hide it through some syntactic sugar...
Double loop stays double loop
+ 1
from itertools import product
for row, col in product(range(50), range(50)):
if data[row][col] == ['255', '255', '0']:
upper_left = (row, col)
break
+ 1
Its a good idea to put all the code inside a while statement, and when yellow,
The control variable equal false?
...
Control=true
While(control)
...
If yellow
Control=false
....
Good or bad idea?
+ 1
Sonic Yes, that’s right! Tuples are even faster for iteration
+ 1
Gabriel Ilie, funny hack, never saw that before. :)