简体   繁体   English

如何从文本文件python中的数字列表中找到平均值

[英]How to find the average from a list of numbers in a text file python

Inside the the text file there is a list of name along side a number which denotes their score in this case. 在文本文件中,有一个名称列表以及一个数字,表示在这种情况下的得分。 is there a way to figure out the average of all the numbers in the text file. 有没有办法找出文本文件中所有数字的平均值。

Code: 码:

print("average calculator")
option = input("option: ")
option_class = input("class: ")
one = "1.txt"
if option.lower() == 'avg' and option_class == '1':
    name = input("name: ")

    with open(one) as f:
        the_list = [int(l.strip().split()[-1]) for l in f if name in l]

        b = sum(the_list)
        length = len(the_list)
        avg = float(b) / length if length else 0
        print (name,"Average is: ", avg)

The section where the code asks the user their name, here it finds all the numbers in the text file that are next to the name, however I am trying to Remove this aspect and allow the code to recognise all the numbers in the file... 代码询问用户名称的部分,在这里查找文本文件中名称旁边的所有数字,但是我试图删除此方面,并允许代码识别文件中的所有数字。 。

Text File: 文本文件:

Matt 3
Jhon 4
Alex 6

From these numbers, [3,4,6] the average should be worked out, NB: the text file can be appended, more names and scores can be added. 从这些数字[3,4,6]中应该得出平均值,注意:可以附加文本文件,可以添加更多名称和分数。

TIA! TIA!

try the pandas library. 尝试熊猫图书馆。

something of the lines: 一些线:

import pandas as pd
df = pd.read_csv('filename.txt', delimiter=' ')
df['Column name'].mean()

it has functionality to filter and group data easily, and is much more useful for this kind of task than writing your own code. 它具有轻松过滤和分组数据的功能,并且比编写自己的代码对这种任务有用得多。 I working with files 100,000 lines +, and can read, group, filter outliers in each group and compute statistics in about 4 lines of code. 我处理的文件超过100,000行,并且可以读取,分组,过滤每个组中的离群值并以大约4行代码计算统计信息。

http://pandas.pydata.org/ http://pandas.pydata.org/

List comprehension: 清单理解:

the_list = [int(l.strip().split()[-1]) for l in f if name in l]

Equivalent: 当量:

with open(one) as f:
    the_list = []   # creating empty list or (array)
    for l in f:     # treversing over the lines in file
        if name in l:  # if input name is in the line do next line
            the_list.append(int(l.strip().split()[-1])) # adding element to list
        else:   # if input name is not in line ignore the line dont use the score
            pass

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

相关问题 如何找到文本文件中字符串中包含的列表中数字的平均值? - How can I find the average of the numbers in a list contained in a string from a text file? 如何从文本文件中找到拆分浮动列表的平均值? - How to find the average of a split float list from a text file? 如何找到 Python 列表的所有数字之间差异的平均值 - How to find the average of the differences between all the numbers of a Python List 你如何在python中找到带引号的数字列表的平均值? - How do you find the average of a list with numbers in quotes, in python? Python - 如何从选定的行号中找到总统的平均身高? - Python - How to find the average height of presidents from selected row numbers? 如何用python评估文本文件中的数字列表 - How to evaluate a list of numbers in a text file with python 从文本文件输入的数字的平均值 - Average of numbers input from a text file Python:如何从数字文件(分数)中读取数据以用作列表并获得高分,低分和平均分,并分组为十分位 - Python: How to read from file of numbers (scores) use as list and get high score, low, and average score, and group into deciles 如何从数字列表中获得平均“差价”? - How to get an average 'spread' from a list of numbers? 如何对 Python 中文本文件中的数字求和 - How to sum numbers from a text file in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM