返回列表中数字的第一个匹配索引

Fin*_*ist 6 python

我有一堆包含时间数据和数字的csv文件,我写了一个函数来返回第一次出现的数字低于阈值(x)这样:

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

除了当我以这种方式循环执行弹跳功能时:

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)
Run Code Online (Sandbox Code Playgroud)

它继续返回零(即使第一个值高于阈值).

我哪里错了?


以下是其中一个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
Run Code Online (Sandbox Code Playgroud)

提前致谢 .

评论后编辑

这是新功能:

def bounce(tickList,x):
    n = 0
    for i in tickList:
        if float(i[1]) < x:
            return n 
        n += 1
Run Code Online (Sandbox Code Playgroud)

如果我打印float(i [1])它会返回正确的数字,因此它正在调用正确的文件.

第二次编辑

发现问题,我正在喂它的"水平"实际上是一个str而不是int,感谢所有看过并帮助过的人.

Jim*_*ion 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
Run Code Online (Sandbox Code Playgroud)

像这样的东西???