Return the index of the first occurrence of a number in a list

I have a bunch of csv files containing temporary data and numbers, I wrote a function to return the first number of a number below the threshold (x) as follows:

def bounce(tickList,x): n = 0 for i in tickList: if float(i[1]) < x: return n break n += 1 

except that when I execute the bounce function execution loop as follows:

 for i in os.listdir(resultDir): if "csv" in i: csvFile = resultDir+i print csvFile with open(csvFile, 'rb') as f: reader = csv.reader(f) tickList = [] for line in reader: tickList.append(line) print bounce(tickList,5) 

it continues to return zero (even if the first value is higher ).

Where am I going wrong?


Here is an example of one of the csv files:

 1373289767.454535,9.9 1373289769.728528,9.9 1373289771.817576,9.9 1373289773.813036,11.7 1373289775.810985,11.7 1373289777.769641,11.7 1373289779.783134,12.2 1373289781.774255,11.8 1373289783.799892,12.0 1373289785.812967,11.4 1373289787.816991,11.4 1373289789.790835,11.3 1373289791.811245,10.9 1373289793.880356,10.8 1373289795.846866,10.7 1373289797.847552,10.6 1373289799.858929,10.6 

Thanks in advance.

EDIT after comments

Here is a new feature:

 def bounce(tickList,x): n = 0 for i in tickList: if float(i[1]) < x: return nn += 1 

If I print float (i [1]), it will return the correct numbers to call the correct files.

SECOND EDIT

found the problem, the "level" that I fed was actually str , not int , thanks to everyone who looked and helped.

+6
source share
5 answers

I strongly suspect your indentation is incorrect, and by mixing spaces and tabs, Python interprets your method as:

 def bounce(tickList,x): n = 0 for i in tickList: if float(i[1]) < x: return n break n += 1 

where n += 1 remains outside the loop and never increases n . Alternatively, n += 1 may be too indented:

 def bounce(tickList,x): n = 0 for i in tickList: if float(i[1]) < x: return n break n += 1 

Your function will return 0 for any case where there is a line with float(i[1]) below x .

You can test such problems by running a script using python -tt scriptname.py , where -tt tells python to search for inconsistent use of tabs and spaces and throw an error if it detects such problems.

You can simplify your code using enumerate() and insert the test by reading the file before:

 for fname in os.listdir(resultDir): if "csv" in fname: csvFile = os.path.join(resultDir, fname) print csvFile with open(csvFile, 'rb') as f: reader = csv.reader(f) for i, row in enumerate(reader) if float(row[1]) < 5: print i break # exit for loop, continue with next file 

The inner for loop can be simplified further with next() and a generator expression:

 with open(csvFile, 'rb') as f: reader = csv.reader(f) print next((i for i, r in enumerate(reader) if float(r[1]) < 5), 'Not found') 

as next() stops the loop as soon as the result is found.

+2
source

You are sad that you need the value below , and that is exactly what is in your code. But after you expect the values above . Change bounce() or threshold for test data :)

 import csv def bounce(tickList,x): n = 0 for i in tickList: #print float(i[1]) if float(i[1]) > x: return n n += 1 csvFile = 'test.csv' print csvFile tickList = [] with open(csvFile, 'rb') as f: reader = csv.reader(f) for line in reader: tickList.append(line) print bounce(tickList,5) 

This code prints 0 .

+1
source
 def bounce(tickList,x): n = 0 for i in tickList: if float(i[1]) < x: return n # return float(i[1) n += 1 return None 

something like that???

0
source

I'm not sure if you need all the data in memory, so there might be something simple:

 reader = enumerate(csv.reader(f)) idx, row = next((r for r in reader if float(r[1][1]) < 5), (None, None)) 
0
source

Make sure that you do not mix spaces and tabs in the identifier.

This works as expected for me (I edited the original function to return both the index and the value of the element).

 def bounce(tickList,x): n = 0 for i in tickList: if float(i[1]) < x: return (n, i[1]) n += 1 
0
source

Source: https://habr.com/ru/post/949860/


All Articles