简体   繁体   English

如何在从特定值开始的for循环中使用枚举function?

[英]How to use enumerate function in for loop starting from a specific value?

I want this code to cycle through the entire dataset and to be performed each time 'CAFE' is in a line.我希望此代码循环遍历整个数据集,并在每次“CAFE”在一行时执行。 Currently, it only occurs for the first appearance of CAFE.目前,它只发生在 CAFE 的第一次出现时。 In other words, I want to specify the start of the enumerate function as the line with CAFE in it, but I am unable to put an argument of start = 'CAFE' inside the enumerate function because the string 'CAFE' cannot be interpreted as an integer.换句话说,我想将枚举 function 的开头指定为其中包含 CAFE 的行,但我无法在枚举 function 中放置 start = 'CAFE' 的参数,因为字符串 'CAFE' 无法解释为一个 integer。

with open('data.txt', 'r') as dataset:
    for line in dataset:
        if 'CAFE' in line:
            for i, lt in enumerate(dataset):
                if i > 1 and i < 130 and i %2 == 0:
                   lt_1 = int(lt, 16)
                   lt_2 = float(str(lt_1))
                   print(lt_2)
             else:
                pass
        else:
            pass 

Well enumerate(iterable[,start=integer]) takes .netger as start not string and It will be helpful if you provide data.txt Well enumerate(iterable[,start=integer])将 .netger 作为开始而不是字符串,如果您提供 data.txt 将会很有帮助

enumerate is equivalent to:枚举等同于:

def enumerate(sequence, start=0):
    n = start
    for elem in sequence:
        yield n, elem
        n += 1

from python docs来自python 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM