排序从最高到最低打印内容以及从最高到最低

时间:2015-07-08 20:48:53

标签: sorting

viewclass= input("choose a class number and either alphabetically, average or highest?")


if viewclass=='1 alphabetically':#alphabetically
    with open('1.txt', 'r') as r:#sorting 
        for line in sorted(r):
             print(line, end='')#prints alphabetically

elif viewclass=='2 alphabetically':#alphabetically
    with open('2.txt', 'r') as r:#sorting 
        for line in sorted(r):
             print(line, end='')#prints alphabetically

elif viewclass=='3 alphabetically':#alphabetically
    with open('3.txt', 'r') as r:#sorting 
        for line in sorted(r):
             print(line, end='')#prints alphabetically



if viewclass=='1 highest':# sorting highest
    file = open("1.txt")#hihest to lowest code works in single and gives out put
    classj = (file.readlines())
    s = []
    for line in sorted(classj):
        classj = (line.rstrip())
        classa = (classj.split(":"))#sort
        score = int(classa[1])
        name = (classa[0])
        s.append( (name,score) )
        s.sort(reverse=True, key=lambda x:x[1])
        for x in s:
            print(x[0],"-",x[1])#prints htl

elif viewclass=='2 highest':# sorting highest
    file = open("2.txt")#hihest to lowest code works in single and gives out put
    classj = (file.readlines())
    s = []
    for line in sorted(classj):
        classj = (line.rstrip())
        classa = (classj.split(":"))#sort
        score = int(classa[1])
        name = (classa[0])
        s.append( (name,score) )
        s.sort(reverse=True, key=lambda x:x[1])
        for x in s:
            print(x[0],"-",x[1])#prints htl

elif viewclass=='3 highest':# sorting highest
    file = open("3.txt")#hihest to lowest code works  givesoutput
    classj = (file.readlines())
    s = []
    for line in sorted(classj):
        classj = (line.rstrip())
        classa = (classj.split(":"))#sort
        score = int(classa[1])
        name = (classa[0])
        s.append( (name,score) )
        s.sort(reverse=True, key=lambda x:x[1])
        for x in s:
            print(x[0],"-",x[1])#prints htl

我的代码按字母顺序排列,但当我要求从最高到最低时,我得到了这个

choose a class number and either alphabetically, average or highest?1 highest
Balsac  - 1# normal scores
Balsac  - 3
Balsac  - 1
Denzil  - 5
Balsac  - 3
Balsac  - 1
Denzil  - 5---- hihest to lowest
Balsac  - 3
Balsac  - 1
Ryan  - 1
Denzil  - 5
Balsac  - 3
Tim  - 2
Balsac  - 1
Ryan  - 1

有谁知道我怎么能得到这个

Denzil  - 5
Balsac  - 3
Tim  - 2
Balsac  - 1
Ryan  - 1

1 个答案:

答案 0 :(得分:0)

不确定;将排序/打印代码移动到解析数据的循环之外;例如:

if viewclass=='1 highest':# sorting highest
    file = open("1.txt")#hihest to lowest code works in single and gives out put
    classj = (file.readlines())
    s = []
    for line in sorted(classj):
        classj = (line.rstrip())
        classa = (classj.split(":"))#sort
        score = int(classa[1])
        name = (classa[0])
        s.append( (name,score) )
    s.sort(reverse=True, key=lambda x:x[1])
    for x in s:
        print(x[0],"-",x[1])#prints htl