0

I currently have a condition as:

if np.any(x <= 0.5):
    print ("right")
else:   
    print ("wrong")

But the problem is I don't want to use np.any() or np.all().

The value of x is in a constantly changing csv file and I want the condition to only apply to the first and last rows of the file of the third column.

What would be the best and most efficient way to implement this?

aydow
  • 3,673
  • 2
  • 23
  • 40
Coshin
  • 53
  • 1
  • 6

2 Answers2

0

Assuming that your numpy array x contains lines of the csv file in its rows, and the values in its columns, you could approach the problem like this (sort of the old fashion way).

first_row_third_col_x = x[0][2]
last_row_third_col_x = x[-1][2]
if first_row_third_col_x , 0.5 or last_row_third_col_x < 0.5:
    print('right')
else:
    print('wrong')
Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
0

From what you wrote, you are really only considering 2 values: the third element of the first row and the third element of the last row. You can read in the first and last line of the CSV, and check just those elements.

import csv

with open('C:/path/to/file.csv') as fp:
    reader = csv.reader(fp)
    first = next(reader)
    for last in reader:
        pass

x1 = float(first[2])
x2 = float(last[2])

if x1 < 0.5 or x2 < 0.5:
    print('right')
else:
    print('wrong')
James
  • 32,991
  • 4
  • 47
  • 70