简体   繁体   English

如何更改此函数以使其返回文件中偶数位数的列表?

[英]How can I change this function so that it returns a list of the number of even digits in a file?

def evens(number_file: TextIO) -> List[int]:
    lst = []
    
    line = number_file.readline().strip()
    while line != '':
        evens = 0
        line = number_file.readline().strip()
        
        while line.isdigit():
            evens = evens + int(line)
            line = number_file.readline().strip()
        lst.append(evens)
        
    return last

in this example the file 'numbers.txt' looks like this:在此示例中,文件“numbers.txt”如下所示:

START开始

1 1

2 2

END结尾

START开始

3 3

END结尾

START开始

4 4

5 5

6 6

END结尾

Each line is either an int or 'START' or 'END' I want to make a function that returns the number of evens in each section so when the code tuns on this file, it should return the list [1, 0, 2].每行都是一个 int 或 'START' 或 'END' 我想创建一个函数来返回每个部分中的偶数数量,因此当代码在此文件上运行时,它应该返回列表 [1, 0, 2] . Could someone please help me?有人可以帮我吗?

import numpy as np

def main():
    line = [1,4,5,6,8,8,9,10]
    evens = np.array()
    for l in line:
        if (l % 2) == 0:
            np.append(evens, l)
    return evens

if __name__ == '__main__':
    main()

Without your txt file and the code to read it in, this is the best I can do.没有你的txt文件和读取它的代码,这是我能做的最好的。

When writing new code, it's a good idea to start small and then add capabilities bit by bit.编写新代码时,最好从小处着手,然后一点一点地添加功能。 eg, write code that works without worrying about sections first, then update to work with sections.例如,首先编写无需担心节的代码,然后更新以使用节。

But anyway, here's some revised code that should work:但无论如何,这里有一些应该可以工作的修改后的代码:

def evens(number_file: TextIO) -> List[int]:
    lst = []
    
    line = number_file.readline().strip()
    while line != '':
        evens = 0

        # ignore all text lines
        while !line.isdigit():
            line = number_file.readline().strip()

        # process number lines
        while line.isdigit():
            if int(line) % 2 == 0:
                evens = evens + 1
            line = number_file.readline().strip()

        lst.append(evens)
        
    return lst

And here's a version that may be simpler ( for loops in Python can often make your life easier, eg when processing every row of a file):这是一个可能更简单的版本(Python 中的for循环通常可以让您的生活更轻松,例如在处理文件的每一行时):

def evens(number_file: TextIO) -> List[int]:
    lst = []

    for row in number_file:
        line = row.strip()
        if line == 'START':
            # new section, start new count
            lst.append(0)
        elif line.isdigit() and int(line) % 2 == 0:
            # update current count (last item in lst)
            lst[-1] += 1

   return lst

暂无
暂无

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

相关问题 我该如何安排这个 function 所以它返回一个包含所有这些列表的列表 - How can I arrange this function so it returns a list with all this lists on it 我怎样才能让这个 function 计算浮点数的位数? - How can I make this function count the number of digits in a float? 如何将浮点数更改为数组,以便可以在Python中将其与零函数连接起来? - How do I change a float number into an array so that I can concatenate it with a zeros function in Python? 如何优化此函数以返回给定范围内具有唯一数字(即无重复数字)的整数数? - How can I optimize this function to return the number of integers with unique digits (ie. no repeating digits) in a given range? 返回数字有多少位的函数digits(n),在python中返回一个随机值 - Function digits(n) that returns how many digits the number has , returns a random value in python 如何编写返回总位数和单个空格的 function - How to write a function which returns the total number of digits and single spaces 我的偶数python函数索引仅返回列表中的第一个偶数。 如何使其返回列表中所有偶数的索引? - My indices of evens python function only returns the first even number in a list. How to make it return the index of all even numbers in a list? 如何删除元组列表中的偶数索引? - How can i remove the even number indexes in a list of tuples? 将浮点数写入文件时,如何减少小数点后的位数? - How can I reduce the number of digits after decimal point when writing floats to file? 如何检查数字中的连续数字是偶数还是奇数? - How to check if consecutive digits in a number are even or odd?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM