问题描述
classs = input("Class [1, 2 or 3] - ")
if clas
data = f.readlines()
for le:
print(line)
found = True
if found == False:
print("False")
这是典型的打印输出:
John = 10
John = 6
John = 4
我需要能够仅使用10、4、6来创建平均值10, 4, 6
因为我需要知道一种隔离其余部分并允许数字继续以创建平均值的方法。
希望有人能够理解并能够提供帮助!
提前致谢!
1楼
如果每行的格式相同,则可以使用string.split
并将其string.split
为int
:
classs = input("Class [1, 2 or 3] - ")
l = []
if classs =='1':
name = input("What is your name? - ")
datafile = '1.txt'
found = False
with open(datafile, 'r') as f:
data = f.readlines()
for line in data:
if name in line:
print(line)
l.append(int(line.split()[2]))
found = True
if found == False:
print("False")
然后浏览数字列表并获得平均值,例如:
total = 0
num = 0
for x in l:
total = total + x
num = num + 1
print(total/num)
2楼
一种方法是从列表中为每个玩家提取最后3个数字(我假设您只需要3个,如果没有,则可以更改此代码以获取更多信息)
Class = input("Class: ")
dataList = []
file = open('class ' + str(Class) + '.txt', "r")
for line in file:
count = 0
record = line
studentList = record.split(': ')
score = studentList[1].strip('\n')
studentList = [studentList[0], score]
if len(dataList) == 0:
dataList.append(studentList)
else:
while count < len(dataList):
if studentList[0] == dataList[count][0]:
if len(dataList[count]) == 4:
dataList[count][3] = dataList[count][2]
dataList[count][2] = dataList[count][1]
dataList[count][1] = score
break
dataList[count].append(score)
break
elif count == len(dataList) - 1:
dataList.append(studentList)
break
count = count + 1
这将为您提供2D阵列。 每个较小的数组将在索引0处代表人员名称,在索引1,2和3处包含三个数字。现在有了这些,您可以简单地求出平均值。
AverageScore = []
# this is the array where the student' record (name and highest score) is saved
# in a list
count = 0
while count < len(dataList):
entry = []
# this is whre each student name and score will be temporarily held
entry.append(dataList[count][0])
# this makes it so the array 'entry' contains the name of every student
Sum = 0
frequency = len(dataList[count])
incount = 1
while incount < frequency:
Sum = Sum + int(dataList[count][incount])
incount = incount + 1
average = Sum / (frequency-1)
entry.append(average)
AverageScore.append(entry)
# this appends the name and average score of the student to the larger array
# 'AverageScore'
count= count + 1
# the count is increased so the process is repeated for the next student
AverageSorted = sorted(AverageScore,key=lambda l:l[1], reverse=True)
# http://stackoverflow.com/questions/18563680/sorting-2d-list-python
# this is code i obtained through someone else's post which arranges the array in descending
# order of scores
count2 = 0
while count2 < len(AverageSorted):
print(AverageSorted[count2][0], ':', AverageSorted[count2][1])
count2 = count2 + 1
# this formats the array so it prints the elements in a list of each student's
# name and score
wind绕而效率低下,但我可以用我对python的一点了解来做到最好:)
3楼
如果这是1.txt
的内容:
John = 1
Joe = 3
John = 7
Joe = 9
Bob = 3
Joe = 8
John = 2
Bob = 9
Roger = 13
将此语句替换为:
name = "John"
_class = 1
with open("%s.txt" % _class, "r") as out:
lines = out.readlines()
scores = []
for line in lines:
if name in line:
# "strip" without arguments strips out all beginning
# and trailing white-space (i.e. " ", "\n", "\r").
line = line.strip()
score = int(line.split(" = ")[1])
scores.append(score)
# If there isn't any scores in this list, then there was no user
# data.
if scores:
# Use python built-in sum to sum the list of scores and
# divide the result by the number of scores gives you the average.
average = sum(scores) / len(scores)
print "%s's average score is %.1f out of %d game(s)." % (
name, average, len(scores))
else:
print "User %s not found." % name