简体   繁体   English

按数字对文件进行排序

[英]Sorting a file numerically

I am new to python and trying to create a maths quiz program towards the end of the program I want to be able to print all the results in the file ( stored in the variable Reverseinfo) in numerical order.我是 python 的新手,并试图在程序结束时创建一个数学测验程序,我希望能够以数字顺序打印文件中的所有结果(存储在变量 Reverseinfo 中)。 This doesn't seem to work , regardless of if I use the .sort function or the sorted() function.这似乎不起作用,无论我是使用 .sort 函数还是 sorted() 函数。 Any help would be appreciated, thanks任何帮助将不胜感激,谢谢

ReverseInfo already has scores and names put into the file ReverseInfo 已经将分数和名称放入文件中

ReverseInfo= (score + (" ") + Name)

ClassANum=open("Class A Results High to Low.txt","a")
    ClassANum.write(ReverseInfo)
    ClassANum.write(" \n")
    ClassANum.close()

ClassBNum=open("Class B Results High to Low.txt","a")
    ClassBNum.write(ReverseInfo)
    ClassBNum.write(" \n")
    ClassBNum.close()

ClassCNum=open("Class C Results High to Low.txt","a")
    ClassCNum.write(ReverseInfo)
    ClassCNum.write(" \n")
    ClassCNum.close()


        if Viewscores=="Y":
            Classresults=input("Which Class would you like to see the results of?")

        if Classresults=="A":
            ClassA=open("Class A Results.txt","r")
            Class=ClassA
            alphabet=ClassA.readlines()
            for line in sorted(alphabet):
                print(line)

        elif Classresults=="B":
            ClassB=open("Class B Results.txt","r")
            Class=ClassB
            alphabet=ClassB.readlines()
            for line in sorted(alphabet):
                print(line)

        elif Classresults=="C":
            ClassC=open("Class C Results.txt","r")
            Class=ClassC
            alphabet=ClassC.readlines()
            for line in sorted(alphabet):
                print(line)

        else:
            print ("That is not valid")

        Numerical=input(" Do you want to see the results alphabetically?")

        if Numerical=="Y":
            NumClassresults=input("Which Class would you like to see the results of?")

        if NumClassresults=="A":
            ClassA=open("Class A Results High to Low.txt","r")
            Class=ClassA
            Hightolow=ClassA.readlines()
            for line in sorted(Hightolow):
                print(line)

        elif NumClassresults=="B":
            ClassA=open("Class B Results High to Low.txt","r")
            Class=ClassB
            Hightolow=ClassB.readlines()
            for line in sorted(Hightolow):
                print(line)

        if NumClassresults=="C":
            ClassA=open("Class C Results High to Low.txt","r")
            Class=ClassC
            Hightolow=ClassC.readlines()
            for line in sorted(Hightolow):
                print(line)

the data stored in Class A is: 10 Jamie 8 Jamie 8 Peter 10 Ham 4 Jack 10 Joseph 9 jamie 9 Yuan 9 Bob 10 John 6 Nash 8 John 10 josh 10 Honey A类存储的数据是:10 Jamie 8 Jamie 8 Peter 10 Ham 4 Jack 10 Joseph 9 jamie 9 Yuan 9 Bob 10 John 6 Nash 8 John 10 josh 10 Honey

there is currently nothing stored in Class b file finally in class c: 9 jamie目前在类 c 中最终在类 b 文件中没有存储任何内容:9 jamie

9 Yuan 9元

9 Bob 9 鲍勃

8 Peter 8 彼得

8 Jamie 8 杰米

4 Jack 4 杰克

10 Joseph 10 约瑟夫

10 John 10 约翰

10 Jamie 10 杰米

10 Ham 10 火腿

Try below (assuming your file looks like Name 100 with many rows)尝试以下(假设您的文件看起来像具有多行的Name 100

with open("Class C Results High to Low.txt","r") as f:
    lines = [(int(line.split(" ")[0]), line.split(" ")[1]) for line in f.readlines()]
    for l in sorted(lines, key=lambda score_name: score_name[0]):
        print l

Please let me know if there are any syntax errors (leave a comment), I couldn't test this - on the phone at the moment.请让我知道是否有任何语法错误(发表评论),我无法测试 - 目前在电话上。

If the file is just lines with one integer per line, Hightolow will be a list of strings (that can be passed to int)如果文件只是每行一个整数的行, Hightolow将是一个字符串列表(可以传递给 int)

You need to pass a key function to sorted (or sort).您需要传递一个键函数来排序(或排序)。 In this case int does exactly what you need.在这种情况下, int正是您所需要的。

sorted(Hightolow, key=int)

ie use it in the loop like this:即在循环中使用它,如下所示:

for line in sorted(Hightolow, key=int):

Alternatively you can make Hightolow a list of ints as you read the file或者,您可以在阅读文件时使Hightolow成为整数列表

Hightolow = [int(x) for x in ClassA]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM