[英]Returning first occurrence index of a number in a list
I have a bunch of csv files containing time data and numbers, I wrote a function to return the first occurrence of a number below a threshold (x) this way : 我有一堆包含时间数据和数字的csv文件,我写了一个函数来返回第一次出现的数字低于阈值(x)这样:
def bounce(tickList,x):
n = 0
for i in tickList:
if float(i[1]) < x:
return n
break
n += 1
except that when I loop the execution of the bounce function this way : 除了当我以这种方式循环执行弹跳功能时:
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 keeps on returning zero (even though the first value is above the threshold ) . 它继续返回零(即使第一个值高于阈值)。
Where am I going wrong ? 我哪里错了?
Here is a sample of one of the csv files : 以下是其中一个csv文件的示例:
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 the new function: 这是新功能:
def bounce(tickList,x):
n = 0
for i in tickList:
if float(i[1]) < x:
return n
n += 1
if I print float(i[1]) it returns the right numbers so it is calling the right files . 如果我打印float(i [1])它会返回正确的数字,因此它正在调用正确的文件。
SECOND EDIT 第二次编辑
found the problem, the "level" I was feeding it was in fact a str and not an int , thanks for everybody who had a look and helped . 发现问题,我正在喂它的“水平”实际上是一个str而不是int ,感谢所有看过并帮助过的人。
I strongly suspect that your indentation is incorrect, and by mixing spaces and tabs Python interprets your method as: 我强烈怀疑你的缩进是不正确的,通过混合空格和制表符,Python会将你的方法解释为:
def bounce(tickList,x):
n = 0
for i in tickList:
if float(i[1]) < x:
return n
break
n += 1
where n += 1
is left outside the loop and never increments n
. 其中
n += 1
留在循环之外 ,永不递增n
。 Alternatively, n += 1
could be indented too far : 或者,
n += 1
可以缩进太多 :
def bounce(tickList,x):
n = 0
for i in tickList:
if float(i[1]) < x:
return n
break
n += 1
Your function would return 0
for any case where there is a row with float(i[1])
below x
. 对于任何一个
float(i[1])
低于x
情况,你的函数会返回0
。
You can test for such problems by running your script with python -tt scriptname.py
, where -tt
tells python to look for inconsistent use of tabs and spaces and raise an error if it finds such problems. 您可以通过使用
python -tt scriptname.py
运行脚本来测试此类问题,其中-tt
告诉python查找选项卡和空格的不一致使用,如果发现此类问题则引发错误。
You can simplify your code by using enumerate()
, and inlining the test, exiting reading the file early : 您可以使用
enumerate()
和内联测试来简化代码,并提前退出文件:
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 down further by using next()
and an generator expression: 通过使用
next()
和生成器表达式,可以进一步简化内部for
循环:
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 looping once a result has been found. as
next()
一旦找到结果就停止循环 。
You've sad you need a value below threshold and that's exactly what in your code. 你很难过,你需要一个低于阈值的值,这正是你的代码中的值。 But after you are expecting value above threshold.
但是在你预期价值高于门槛之后。 Change
bounce()
or threshold ot test data:) 更改
bounce()
或阈值ot测试数据:)
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
. 此代码打印
0
。
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 this??? 这样的事情???
I'm not convinced you need the entire data into memory, so maybe something as simple as: 我不相信你需要将整个数据存入内存,所以也许就像这样简单:
reader = enumerate(csv.reader(f))
idx, row = next((r for r in reader if float(r[1][1]) < 5), (None, None))
Make sure that you are not mixing spaces and tabs in the identation. 确保您没有在标识中混合空格和制表符。
This works as expected for me (I edited the original function to return both the index and the element value). 这对我来说是预期的(我编辑了原始函数以返回索引和元素值)。
def bounce(tickList,x):
n = 0
for i in tickList:
if float(i[1]) < x:
return (n, i[1])
n += 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.