查找1000行文本的总工资和平均工资

时间:2014-06-20 00:43:31

标签: python sum average

我需要编写一个python程序,读取1000行文本,并获取每个教授类别(助理,助理,全员,所有教员)的工资总额和平均值。这是问题所在:

  

大学的员工工资是在   http://cs.armstrong.edu/liang/data/Salary.txt。文件中的每一行   由教师的名字,姓氏,职级和薪水组成(见   练习13.16)。编写一个程序来显示总薪水   助理教授,副教授,全职教授等等   教师分别显示助理的平均工资   教授,副教授,全职教授和所有教师,   分别

这是我到目前为止的代码:

import urllib.request

def main():
    infile = urllib.request.urlopen('http://cs.armstrong.edu/liang/data/Salary.txt')
    s = infile.read().decode().splitlines()

    asstTotal = 0
    asstCount = 0
    asscTotal = 0
    asscCount = 0
    fullTotal = 0
    fullCount = 0

    for line in s:
        if "assistant" in line:
            asstCount += 1
            asstTotal += int(float(s[3]))

        if "associate" in line:
            asscCount += 1
            asscTotal += int(float(s[3]))

        if "full" in line:
            fullCount += 1
            fullTotal += int(float(s[3]))

    allFacTotal = asstTotal + asscTotal + fullTotal
    avgAsst = int(asstTotal / asstCount)
    avgAssc = int(asscTotal / asscCount)
    avgFull = int(fullTotal / fullCount)
    avgAll = int(allFacTotal / 1000)

    print("Total assistant professor salary is", asstTotal)
    print("Total associate professor salary is", asscTotal)
    print("Total full professor salary is", fullTotal)
    print("Total faculty salary is", allFacTotal)
    print("Average assistant professor salary is", avgAsst)
    print("Average associate professor salary is", avgAssc)
    print("Average full professor salary is", avgFull)
    print("Average faculty salary is", avgAll)

main()

我不确定如何从相应的教授职级中获得薪水并总计和平均。任何意见都会非常感激!

结果:

Total assistant professor salary is 24306418
Total associate professor salary is 27235856
Total full professor salary is 27631726
Total faculty salary is 79174000
Average assistant professor salary is 79174
Average associate professor salary is 79174
Average full professor salary is 79174
Average faculty salary is 79174

3 个答案:

答案 0 :(得分:1)

这可以通过为字典中的每个位置建立一个薪酬列表,然后简单地将总和除以长度来解释任何类型的等级。

我的例子:

import urllib.request

def main():
    infile = urllib.request.urlopen('http://cs.armstrong.edu/liang/data/Salary.txt')
    s = infile.read().decode().splitlines()

    pays = {}

    for line in s:
        *name, rank, pay = line.split()
        pays[rank] = pays.get(rank, []) + [float(pay)]

    for rank, sals in pays.items():
        avg = sum(sals) / len(sals)
        print("{:>10} ({:3}): ${:,.2f}".format(rank.title(), len(sals), avg))


if __name__ == '__main__':
    main()

输出:

Associate (344): $83,849.26
     Full (349): $102,229.37
Assistant (307): $65,949.55

答案 1 :(得分:0)

import urllib.request

def main():
    infile = urllib.request.urlopen("http://cs.armstrong.edu/liang/data/Salary.txt")

    asstSalTotal = 0
    asstTotal = 0
    asscSalTotal = 0
    asscTotal = 0
    fullSalTotal = 0
    fullTotal = 0

    for line in infile.readlines():
        data = line.decode().split()
        designation = data[2]
        if designation == "assistant":
            asstSalTotal += float(data[3])
            asstTotal += 1
        elif designation == "associate":
            asscSalTotal += float(data[3])
            asscTotal += 1
        elif designation == "full":
            fullSalTotal += float(data[3])
            fullTotal += 1

    allSalTotal = asstSalTotal + asscSalTotal + fullSalTotal
    allTotal = asstTotal + asscTotal + fullTotal

    print("Total Salary:\n\tAssistant:\t%d\n\tAssociate:\t%d\n\tFull:\t%d\n\tAll:\t%d\n\nAvg Salary:\n\tAssistant:\t%d\n\tAssociate:\t%d\n\tFull:\t%d\n\tAll:\t%d\n" % (asstSalTotal, asscSalTotal, fullSalTotal, allSalTotal, asstTotal, asscTotal, fullTotal, allTotal))


main()

答案 2 :(得分:0)

在python2.7中,使用urllib2.urlopen并将标题与表格一起分组。

在python3中,使用urllib.request.urlopen并将print ..更改为print(..)

import urllib2

rs = urllib2.urlopen('http://cs.armstrong.edu/liang/data/Salary.txt')
title_salaries = {}

for line in rs.read().split("\n"):
    _, _, title, salary = line.split(' ')
    if not title_salaries.has_key(title):
        title_salaries[title] = []

    title_salaries[title].append(float(salary))

for title, salaries in title_salaries.items():
    print title
    print 'avg:', sum(salaries) / len(salaries)
    print 'total:', sum(salaries)
    print