【问题标题】:if next(item) moves to the next item in a list, what is the eqvilant of next next(item) python如果 next(item) 移动到列表中的下一项,则相当于 next(item) python
【发布时间】:2015-01-18 05:43:29
【问题描述】:

这里是上下文的代码。

    def processScores( file, score):
#opens file using with method, reads each line with a for loop. If content in line
#agrees with parameters in  elif statements, executes code in if statment. Otherwise, ignores line    

    with open(file,'r') as f:
        for line in f:  #starts for loop for all if statements
            line = line.strip()
            if line.isdigit():
                start = int(line)
                score.initialScore(start)
                print(line)#DEBUG TEST**** #checks if first line is a number if it is adds it to intial score

            elif len(line) == 0:
                print(line)#DEBUG TEST****
                continue        #if a line has nothing in it. skip it  

            elif line == 'o' or line == 'O':
                amount = next(f)
                print(line)#DEBUG TEST****
                score.updateOne(amount) #if line contains single score marker, Takes content in next line and
                                        #inserts it into updateOne
            elif line == 'm'or line == 'M':
                scoreList = next(f)
                lst = []
                for item in scoreList:
                    print(line)#DEBUG TEST****
                    lst.append(item)
                    score.updateMany(lst) # if line contains list score marker, creates scoreList variable and places the next line into  that variable
                                          # creates lst variable and sets it to an empty list
                                          # goes through the next line with the for loop and appends each item in the next line to the empty list
                                          # then inserts newly populated lst into updateMany

            elif line == 'X':
                print(line)#DEBUG TEST****
                score.get(self)
                score.average(self) # if line contains terminator marker. prints total score and the average of the scores.
                                    # because the file was opened with the 'with' method. the file closes after 

我正在尝试的想法是使用如下所示的文件:

50

30

40

M

10 20 30

o

5

1 2 3

X

如果代码看到“O”或“o”,那么它需要在代码中取下一行并将其添加到运行分数中。但是下一行是空格...所以我需要跳到“O”或“o”之后的第二行。

我正在考虑为此做一个例外,但在我走这条路之前,我想看看是否有人可能知道更好的方法。

【问题讨论】:

    标签: python file next


    【解决方案1】:

    如果您想继续 f 跳过仅包含空格的项目,

    while True:
        x = next(f).strip()
        if x: break
    

    会起作用,也会起作用

    for x in f:
        x = x.strip()
        if x: break
    

    不同的是,如果f 中的非全空格项后面有 no 怎么办。前者将退出StopIteration 异常,后者退出for 循环,但x 设置为''。选择你的毒药(你更愿意处理哪种退出形式)并相应地编码!

    【讨论】:

      【解决方案2】:

      怎么样:

      For line in lines:
        if type(line) == 'int':
           oneCount += line
        elif type(line) == 'list':
           manyCount.append(line)
        elif type(line) == 'str' and line != 'x':
           continue
        elif type(line) == None:
           continue
        else:
           print scores
      

      【讨论】:

        【解决方案3】:

        状态机是思考这个问题的一个有用模型。

        代码有3种状态:

        1. 读取命令代码。
        2. 添加单个分数(在“O”上)。
        3. 添加多个分数(在“M”上)。

        通过使变量保持当前状态,您可以处理输入而无需向前跳过。

        现在,空行似乎没有任何作用,因此您可以像这样从输入中删除它们:

        ...
        non_empty_lines = (line for line in f if line.strip())
        for line in non_empty_lines:
          ... do your thing ...
        

        生成器表达式将过滤所有空格的行。

        如果由于某种原因您不能使用生成器表达式,请在循环内执行:

        ...
        for line in f:
          if not line.strip():
            continue
          ...
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-22
          • 1970-01-01
          • 2019-09-08
          • 1970-01-01
          相关资源
          最近更新 更多