Reputation: 1
I am trying to enter/exit a while loop if all the values in a column of an array match the column of another array.
For Example, I have;
import numpy as np
ColumnA = np.array([[1], [1], [1], [-1], [-1], [1]])
ColumnB = np.array([[1], [1], [1], [-1], [-1], [-5]])
iterations = 10
count = 0
while iterations != count:
count = count + 1
print (count)
while ColumnA.all() != ColumnB.all():
ColumnB = ColumnB[0, 5] + 1
print(ColumnB)
print("Exit")
print(count)
What I would expect is for this code to enter that nested while loop, since ColumnA[0, 5] does not equal ColumnB[0, 5], and then for ColumnB[0, 5] to increase by 1 until it matchs ColumnA[0, 5] at which point it exits the while loop.
This Code does not enter the while loop, so I'm assuming it thinks ColumnA.all() is the same as ColumnB.all(). My guess is that it's just looking at the first value of the array, but I want it to match all parts of the array to exit the loop.
Thank you for any help.
Upvotes: 0
Views: 125
Reputation: 1337
With ColumnA.all()
you're only checking whether all elements in ColumnA
evaluate to true. That is the case in ColumnA
as well as ColumnB
. So ColumnA.all() == ColumnB.all()
evaluates to True == True
, which of course is true. You need to compare elementwise and then check if all elements of the boleean vector that you get are True
:
while not all(ColumnA == ColumnB):
...
Also your index is wrong. Your arrays are 6x1, so if you want the last element you need [5, 0]
not [0, 5]
.
Upvotes: 0