Python,排序平均值

Python,排序平均值,python,algorithm,sorting,Python,Algorithm,Sorting,我对文本文件中的数据排序有一半的问题。我想从3个加权数中找出平均数,并打印出该单独海损所属的文本文件中的名称 我已经设法做到这一点的最高平均水平,但我有一些问题,找到最低的平均水平 highestAverage = 0 highestAvgDepName = 0 if choice2 == 1: calculateAverage(allDepartments,choice2) for x in range (10):

我对文本文件中的数据排序有一半的问题。我想从3个加权数中找出平均数,并打印出该单独海损所属的文本文件中的名称

我已经设法做到这一点的最高平均水平,但我有一些问题,找到最低的平均水平

highestAverage = 0
    highestAvgDepName = 0      
    if choice2 == 1:
        calculateAverage(allDepartments,choice2)
        for x in range (10):
            if highestAverage < calculateAverage(allDepartments, x):
                highestAverage = calculateAverage(allDepartments, x)
                highestAvgDepName = x
文件中的示例文本

Shoes 210 130 67
Bags  167 321 459
文本文件中的位置0是部门名称。文件中的位置1为“良好投票”。立场2“公平投票”。第3位“票数不足”

列表中平均值最高的是鞋子,平均值为2.351,最低的是包,平均值为1.692

我定义了一个函数来计算平均值,并对最高平均值进行了调用,没有任何问题

highestAverage = 0
    highestAvgDepName = 0      
    if choice2 == 1:
        calculateAverage(allDepartments,choice2)
        for x in range (10):
            if highestAverage < calculateAverage(allDepartments, x):
                highestAverage = calculateAverage(allDepartments, x)
                highestAvgDepName = x
您可以使用以下选项:

highestAverage = 0
highestAvgDepName = 0      
if choice2 == 1:
    calculateAverage(allDepartments,choice2)
    for x in range (10):
        if highestAverage > calculateAverage(allDepartments, x):
            highestAverage = calculateAverage(allDepartments, x)
            highestAvgDepName = x

您可以将您的部门存储在一个按平均值键入的字典中,然后将您的平均值存储在一个列表中,并对列表进行排序,以获得最高和最低的平均值

def calculateAverage(choice2):
    total = choice2[0] + choice2[1] + choice2[2]
    return((choice2[0]*3 + choice2[1]*2 + choice2[2])/total)

d={}
l = []
with open(file,'r') as f:
    for i in f:
        tmp = i.split()
        avg = calculateAverage([float(j) for j in tmp[1:]])
        d[avg] = tmp[0]
        l.append(avg)
    l = sorted(l)
print 'Highest: {} {:.4}'.format(d[highest], highest)
print 'Lowest: {} {:.4}'.format(d[lowest], lowest)

Highest: Shoes 2.351
Lowest: Bags 1.692
或者你可以这样做,占用更少的内存

def calculateAverage(choice2):
    total = choice2[0] + choice2[1] + choice2[2]
    return((choice2[0]*3 + choice2[1]*2 + choice2[2])/total)
d={}
l = []
highest = -100000
lowest = 100000

with open(file,'r') as f:
    for i in f:
        tmp = i.split()
        avg = calculateAverage([float(j) for j in tmp[1:]])
        d[avg] = tmp[0]
        if avg > highest:
            highest = avg
        if avg < lowest:
            lowest = avg
print 'Highest: {} {:.4}'.format(d[highest], highest)
print 'Lowest: {} {:.4}'.format(d[lowest], lowest)
def calculateAverage(选项2):
总计=选项2[0]+选项2[1]+选项2[2]
返回((choice2[0]*3+choice2[1]*2+choice2[2])/总计)
d={}
l=[]
最高=-100000
最低=100000
打开(文件,'r')作为f:
对于f中的i:
tmp=i.split()
平均值=计算平均值([tmp中j的浮动(j)[1:]]
d[avg]=tmp[0]
如果平均值>最高值:
最高=平均值
如果平均值<最低值:
最低=平均值
打印'Highest:{}{:.4}'。格式(d[Highest],Highest)
打印'lower:{}{:.4}'。格式(d[lower],lower)

我尝试反转操作员,但它给了我错误的部门,平均值为0。000@s.kelly你能在你的问题中把它贴在可读的地方吗?你能提供关于你的解析代码的信息吗?我想,那边也有个问题。谢谢!我还没有学过这么详细的词典,但它很管用。在打印语句时,是否有办法将最高和最低值更改为3sf?(即2.351)@s.kelly是的,您可以使用格式打印带有约束的字符串。这里有一个例子,你可以阅读更多。它在打印复杂字符串时非常有用。