简体   繁体   English

用python计算平均值

[英]Calculating the average in python

Am Writing a program that prompts for a file name, then opens that file and reads through the file, looking for lines of the form: Am正在编写一个提示输入文件名的程序,然后打开该文件并读取该文件,以查找以下形式的行:

X-DSPAM-Confidence: 0.8475 X-DSPAM-置信度:0.8475

I want to count these lines and extract the floating point values from each of the lines and compute the average of those values. 我想对这些行进行计数,并从每行中提取浮点值,然后计算这些值的平均值。 Can I please get some help. 请给我一些帮助。 I just started programming so I need something very simple. 我刚刚开始编程,所以我需要一些简单的东西。 This is the code I have already written. 这是我已经编写的代码。

fname = raw_input("Enter file name: ")
    if len(fname) == 0:
        fname = 'mbox-short.txt'
    fh = open(fname,'r')
    count = 0
    total = 0
    #Average = total/num of lines
    for line in fh:
        if not line.startswith("X-DSPAM-Confidence:"): continue
        count = count+1
        print line

Iterate over the file (using the context manager ("with") handles the closing automatically), looking for such lines (like you did), and then read them in like this: 遍历文件(使用上下文管理器(“ with”)自动处理关闭),查找此类行(就像您所做的那样),然后像这样读取它们:

fname = raw_input("Enter file name:")
if not fname:
    fname = "mbox-short.txt"
scores = []
with open(fname) as f:
    for line in f:
        if not line.startswith("X-DSPAM-Confidence:"):
            continue
        _, score = line.split()
        scores.append(float(score))
print sum(scores)/len(scores)

Or a bit more compact: 或更紧凑:

mean = lambda x: sum(x)/len(x)
with open(fname) as f:
    result = mean([float(l.split()[1]) if line.startswith("X-DSPAM-Confidence:") for l in f])

Try: 尝试:

total += float(line.split(' ')[1])

so that total / count gives you the answer. 这样total / count为您提供答案。

A program like the following should satisfy your needs. 如下所示的程序应该可以满足您的需求。 If you need to change what the program is looking for, just change the PATTERN variable to describe what you are trying to match. 如果您需要更改程序寻找的内容,只需更改PATTERN变量以描述您要匹配的内容。 The code is written for Python 3.x but can be adapted for Python 2.x without much difficulty if needed. 该代码是为Python 3.x编写的,但如果需要,可以很容易地适用于Python2.x。

Program: 程序:

#! /usr/bin/env python3
import re
import statistics
import sys


PATTERN = r'X-DSPAM-Confidence:\s*(?P<float>[+-]?\d*\.\d+)'


def main(argv):
    """Calculate the average X-DSPAM-Confidence from a file."""
    filename = argv[1] if len(argv) > 1 else input('Filename: ')
    if filename in {'', 'default'}:
        filename = 'mbox-short.txt'
    print('Average:', statistics.mean(get_numbers(filename)))
    return 0


def get_numbers(filename):
    """Extract all X-DSPAM-Confidence values from the named file."""
    with open(filename) as file:
        for line in file:
            for match in re.finditer(PATTERN, line, re.IGNORECASE):
                yield float(match.groupdict()['float'])


if __name__ == '__main__':
    sys.exit(main(sys.argv))

You may also implement the get_numbers generator in the following way if desired. 如果需要,您还可以按照以下方式实现get_numbers生成器。

Alternative: 替代方案:

def get_numbers(filename):
    """Extract all X-DSPAM-Confidence values from the named file."""
    with open(filename) as file:
        yield from (float(match.groupdict()['float'])
                    for line in file
                    for match in re.finditer(PATTERN, line, re.IGNORECASE))

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

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