Reputation: 7942
I want to determine if every element in my n x n list is the same. i.e. [[0,0],[0,0]]
returns true but [[0,1],[0,0]]
will return false. I was thinking of stoping immediately when I find an element that is not the same as the first element. i.e:
n=L[0][0]
m=len(A)
for i in range(m):
for j in range(m):
if
L[i][j]==n: -continue the loop-
else: -stop the loop-
I would like to stop this loop if L[i][j]!==n
and return false, otherwise return true. How would I go about implementing this?
Upvotes: 79
Views: 425806
Reputation: 596743
There are several ways to do it:
n = L[0][0]
m = len(A)
found = False
for i in range(m):
if found:
break
for j in range(m):
if L[i][j] != n:
found = True
break
Pros: easy to understand Cons: additional conditional statement for every loop
n = L[0][0]
m = len(A)
try:
for x in range(3):
for z in range(3):
if L[i][j] != n:
raise StopIteration
except StopIteration:
pass
Pros: very straightforward Cons: you use Exception outside of their semantic
def is_different_value(l, elem, size):
for x in range(size):
for z in range(size):
if l[i][j] != elem:
return True
return False
if is_different_value(L, L[0][0], len(A)):
print "Doh"
pros: much cleaner and still efficient cons: yet feels like C
def is_different_value(iterable):
first = iterable[0][0]
for l in iterable:
for elem in l:
if elem != first:
return True
return False
if is_different_value(L):
print "Doh"
pros: still clean and efficient cons: you reinvdent the wheel
any()
:def is_different_value(iterable):
first = iterable[0][0]
return any(cell != first for col in iterable for cell in col)
if is_different_value(L):
print "Doh"
pros: you'll feel empowered with dark powers cons: people that will read you code may start to dislike you
Upvotes: 69
Reputation: 1
I know this question was asked a long time ago, but if that could help anyone else, here's my answer:
I find it easier to understand with the use of a while loop and it will stop both for loops that way. The code below also return the True/False as asked when the function check_nxn_list() is called. Some parameters may have to be added in the function call.
def check_nxn_list():
state = True
n=L[0][0]
m=len(A)
while state == True:
for i in range(m):
for j in range(m):
if L[i][j]!=n:
state = False
break
return state
The break at the end of the while loop is required to end the loop even if state stays True.
Upvotes: -2
Reputation: 12212
In order to jump out of a loop, you need to use the break statement.
n=L[0][0]
m=len(A)
for i in range(m):
for j in range(m):
if L[i][j]!=n:
break;
Here you have the official Python manual with the explanation about break and continue, and other flow control statements:
http://docs.python.org/tutorial/controlflow.html
EDITED: As a commenter pointed out, this does only end the inner loop. If you need to terminate both loops, there is no "easy" way (others have given you a few solutions). One possiblity would be to raise an exception:
def f(L, A):
try:
n=L[0][0]
m=len(A)
for i in range(m):
for j in range(m):
if L[i][j]!=n:
raise RuntimeError( "Not equal" )
return True
except:
return False
Upvotes: -3
Reputation: 29103
Try to simply use break statement.
Also you can use the following code as an example:
a = [[0,1,0], [1,0,0], [1,1,1]]
b = [[0,0,0], [0,0,0], [0,0,0]]
def check_matr(matr, expVal):
for row in matr:
if len(set(row)) > 1 or set(row).pop() != expVal:
print 'Wrong'
break# or return
else:
print 'ok'
else:
print 'empty'
check_matr(a, 0)
check_matr(b, 0)
Upvotes: 5
Reputation: 62
To achieve this you would do something like:
n=L[0][0]
m=len(A)
for i in range(m):
for j in range(m):
if L[i][j]==n:
//do some processing
else:
break;
Upvotes: 0
Reputation: 70031
Others ways to do the same is:
el = L[0][0]
m=len(L)
print L == [[el]*m]*m
Or:
first_el = L[0][0]
print all(el == first_el for inner_list in L for el in inner_list)
Upvotes: 0
Reputation: 46607
Use break
and continue
to do this. Breaking nested loops can be done in Python using the following:
for a in range(...):
for b in range(..):
if some condition:
# break the inner loop
break
else:
# will be called if the previous loop did not end with a `break`
continue
# but here we end up right after breaking the inner loop, so we can
# simply break the outer loop as well
break
Another way is to wrap everything in a function and use return
to escape from the loop.
Upvotes: 125