首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我想按字母顺序对我的文件进行排序,列出每个学生在测试中的最高分

我想按字母顺序对我的文件进行排序,列出每个学生在测试中的最高分
EN

Stack Overflow用户
提问于 2015-07-12 01:34:38
回答 1查看 159关注 0票数 0
代码语言:javascript
复制
def byalphabet():
  if classchoice == '1':
    with open("newFile.txt", "r+")as file:
        file.seek(0)
        scores = file.readlines()
    alphabetical = []
    for i in range (0, len(scores)):
        line = scores[i].rstrip('\n')
        alphabetical.append(line)

    alphabetical = sorted(alphabetical)
    for i in range (0, len(alphabetical)):
        print (alphabetical[i])
    user_scores = {}
    for line in scores:
        name, score = line.rstrip('\n').split(' - ')
        score = int(score)
        if name not in user_scores or user_scores[name] < score:
            user_scores[name] = score
    for name in sorted(user_scores):
        print(name, '-', user_scores[name])

elif classchoice == '2':
    with open("newFile2.txt", "r+")as file:
        file.seek(0)
        scores = file.readlines()
    alphabetical = []
    for i in range (0, len(scores)):
        line = scores[i].rstrip('\n')
        alphabetical.append(line)

    alphabetical = sorted(alphabetical)
    for i in range (0, len(alphabetical)):
        print (alphabetical[i])
    user_scores = {}
    for line in scores:
        name, score = line.rstrip('\n').split(' - ')
        score = int(score)
        if name not in user_scores or user_scores[name] < score:
            user_scores[name] = score
    for name in sorted(user_scores):
        print(name, '-', user_scores[name])

else:
    with open("newFile3.txt", "r+")as file:
        file.seek(0)
        scores = file.readlines()
    alphabetical = []
    for i in range (0, len(scores)):
        line = scores[i].rstrip('\n')
        alphabetical.append(line)
    alphabetical = sorted(alphabetical)
    for i in range (0, len(alphabetical)):
        print (alphabetical[i])        
    user_scores = {}
    for line in scores:
        name, score = line.rstrip('\n').split(' - ')
        score = int(score)
        if name not in user_scores or user_scores[name] < score:
            user_scores[name] = score
    for name in sorted(user_scores):
        print(name, '-', user_scores[name]) `    

我的程序似乎输出了比所需的更多的值

代码语言:javascript
复制
Aaron - 4
Joeseph - 10
Joeseph - 3
Joeseph - 4
Joeseph - 5
Test1 - 1
Test1 - 10
Test1 - 6
Test2 - 4
Zzron - 1
Aaron - 4
Joeseph - 10
Test1 - 10
Test2 - 4
Zzron - 1
EN

回答 1

Stack Overflow用户

发布于 2015-07-12 03:14:56

更详细的版本:

代码语言:javascript
复制
import io

file0 = io.StringIO('''Aaron - 4
Joeseph - 10
Joeseph - 3
Joeseph - 4
Joeseph - 5
Test1 - 1
Test1 - 10
Test1 - 6
Test2 - 4
Zzron - 1
Aaron - 4
Joeseph - 10
Test1 - 10
Test2 - 4
Zzron - 1''')

def read_file(fle):
    data = {}
    for line in fle.readlines():
        splt = line.split(' - ')
        if len(splt) == 2:
            name, score = splt[0], int(splt[1])
            if not name in data:
                data[name] = score
                continue
            else:
                if score > data[name]:
                    data[name] = score
                    continue
    return data

def print_data(data):
    for name, score in sorted(data.items()):
        print('{} - {}'.format(name, score))


data = read_file(file0)
print_data(data)

输出:

代码语言:javascript
复制
Aaron - 4
Joeseph - 10
Test1 - 10
Test2 - 4
Zzron - 1
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31359929

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档