如何从Python文本文件计算平均分数
我想了解如何从文本文件中整理平均分数,文本文件如下所示:如何从Python文本文件计算平均分数,python,Python,我想了解如何从文本文件中整理平均分数,文本文件如下所示: Matt 3 John 6 Gucci 7 请注意,名称和分数之间只有一个空格 从这个文本文件中,我想计算出整个文件内容的平均值(分数) 我当前的代码如下: print("Write 'avg' to work out average of scores") option = input ("\nEnter your Option: ") list = [] option_class = input("
Matt 3
John 6
Gucci 7
请注意,名称和分数之间只有一个空格
从这个文本文件中,我想计算出整个文件内容的平均值(分数)
我当前的代码如下:
print("Write 'avg' to work out average of scores")
option = input ("\nEnter your Option: ")
list = []
option_class = input("\nWhich Class do you wish to preview: ")
one = "1.txt"
two = "2.txt"
if option =='avg'.lower():
if option_class == "1":
name = input("\nWhat is your name? - ")
found = False
with open(one, 'r') as f:
data = f.readlines()
for line in data:
if name in line:
print("\n",line)
list.append(int(line.split()[2]))
found = True
if found == False:
print("\nFalse")
b = sum(list)
len(list)
avg = float(sum(list))/len(list)
Matt 3
John 6
Gucci 7
这是针对类1中的内容,只是为了参数起见,我们将说文本文件1的内容如下:
print("Write 'avg' to work out average of scores")
option = input ("\nEnter your Option: ")
list = []
option_class = input("\nWhich Class do you wish to preview: ")
one = "1.txt"
two = "2.txt"
if option =='avg'.lower():
if option_class == "1":
name = input("\nWhat is your name? - ")
found = False
with open(one, 'r') as f:
data = f.readlines()
for line in data:
if name in line:
print("\n",line)
list.append(int(line.split()[2]))
found = True
if found == False:
print("\nFalse")
b = sum(list)
len(list)
avg = float(sum(list))/len(list)
Matt 3
John 6
Gucci 7
是否有更简单的方法来缩短代码以提高效率,或者如何使代码能够读取文本文件,因为名称和分数之间只有一个空格。与其简单地打开文件并计算其内容中数字的平均值(假设每行的形式为[name][space][number]),不如使用“name-score”:
至于只需打开文件并计算其内容中数字的平均值(假设每一行的格式为[name][space][number]):
您不必执行
f.readlines()
和'avg'。lower()
看起来不像您打算执行的操作,.lower()
必须用于选项
变量。同时避免像使用list
那样隐藏内置函数和关键字
示例代码:
print(...)
option = input(...)
option_class = input(...)
if option.lower() == 'avg' and option_class == '1': # .lower() on option.
name = input(...)
with open(one) as f: # '-r' is the default so omitting it is cleaner
# 1. iterate the file handler directly without .readlines().
# 2. use list comprehension
# 3. avoid shadowing "list" by using, say, "the_list"
the_list = [int(l.strip().split()[-1]) for l in f if l.strip() and name in l.split()]
b = sum(the_list)
length = len(the_list)
# 1. dont repeat sum() & len() calls
# 2. default to 0 avg to avoid division by 0.
avg = float(b) / length if length else 0
您不必执行
f.readlines()
和'avg'。lower()
看起来不像您打算执行的操作,.lower()
必须用于选项
变量。同时避免像使用list
那样隐藏内置函数和关键字
示例代码:
print(...)
option = input(...)
option_class = input(...)
if option.lower() == 'avg' and option_class == '1': # .lower() on option.
name = input(...)
with open(one) as f: # '-r' is the default so omitting it is cleaner
# 1. iterate the file handler directly without .readlines().
# 2. use list comprehension
# 3. avoid shadowing "list" by using, say, "the_list"
the_list = [int(l.strip().split()[-1]) for l in f if l.strip() and name in l.split()]
b = sum(the_list)
length = len(the_list)
# 1. dont repeat sum() & len() calls
# 2. default to 0 avg to avoid division by 0.
avg = float(b) / length if length else 0
非常感谢这是完美的,我现在明白了!再次感谢。非常感谢这太完美了,我现在明白了!再次感谢。你所需要做的就是使用一个空名字。还请注意,这里有一个潜在的bug-假设您有两个名字:“Sam”和“Samantha”,搜索“Sam”时还将包括“Samantha”。@DeanFenster编辑并修复了列表理解,以解决您提到的问题。谢谢。你只需要用一个空名字。还请注意,这里有一个潜在的bug-假设您有两个名字:“Sam”和“Samantha”,搜索“Sam”时还将包括“Samantha”。@DeanFenster编辑并修复了列表理解,以解决您提到的问题。谢谢