Python 3.x 按2个字段排序,按列表中的第二个字段排序

Python 3.x 按2个字段排序,按列表中的第二个字段排序,python-3.x,Python 3.x,嗨,我正在尝试对从数据文件读取的列表中的两个字段进行排序。现在,虽然排序工作正常,但我试图实现的是:我有三个字段——Classcode、Name、Score。现在,程序首先在名称字段上排序,然后按升序在分数字段上排序。但是,我无法让它在分数字段中按降序排序 我的密码是: import random q=0 ans=0 studentnames = [] ascending = [] classcode = input("Enter your class (1,2 or 3): ") if

嗨,我正在尝试对从数据文件读取的列表中的两个字段进行排序。现在,虽然排序工作正常,但我试图实现的是:我有三个字段——Classcode、Name、Score。现在,程序首先在名称字段上排序,然后按升序在分数字段上排序。但是,我无法让它在分数字段中按降序排序

我的密码是:

import random
q=0
ans=0
studentnames = []
ascending = []


classcode = input("Enter your class (1,2 or 3): ")

if classcode == '1':
name=input("Enter your name: ")
n= int(input("Enter the number of questions you want to attempt: "))

#writing details to a txt file group1

text_file1 = open("data.dat", "a")


while q<n:                  
    c = random.randint(1,3)
    print("c is: ", c)

    if c==1:   
        f="+"

        num1 = random.randint(0,12)
        print("num1 is: ", num1)

        num2 = random.randint(0,10)
        print("num2 is: ", num2)

        product=num1*num2
        difference=num1-num2
        total=num1+num2

        d=total

        q+=1

        print("\n Question ", q)
        print ("What is  ",num1,f,num2," ? ")

        l=int(input("Enter Your Answer: "))
        if l==d:
            print ("\nCongratulations! your answer is right.\n")
            ans+=1
        else:
            print("\n I'm afraid your answer is wrong. The correct one is" ,d)            

    elif c==2:         
        f="*"

        num1 = random.randint(0,12)
        print("num1 is: ", num1)

        num2 = random.randint(0,10)
        print("num2 is: ", num2)

        product=num1*num2
        difference=num1-num2
        total=num1+num2

        d=product


        q+=1

        print("\n Question ", q)
        print ("What is  ",num1,f,num2," ? ")

        l=int(input("Enter Your Answer: "))
        if l==d:
            print ("\nCongratulations! your answer is right.\n")
            ans+=1
        else:
            print("\n I'm afraid your answer is wrong. The correct one is" ,d)

    else:         
        f="-"

        num1 = random.randint(0,12)
        print("num1 is: ", num1)

        num2 = random.randint(0,10)
        print("num2 is: ", num2)

        product=num1*num2
        difference=num1-num2
        total=num1+num2

        d=difference

        q+=1 # counter for questions

        print("\n Question ", q)
        print ("What is  ",num1,f,num2," ? ")

        l=int(input("Enter Your Answer: "))
        if l==d:
            print ("\nCongratulations! your answer is right.\n")
            ans+=1 # counter for answers
        else:
            print("\n I'm afraid your answer is wrong. The correct one is" ,d)

# calculating the percentage
x=float(ans)
y=float(q)
z=ans*100/q
cp=int(z)
print("Your Correct percentage = ",cp,"%\n")

#store details in a list
scores=[classcode,name,cp]
print(scores)

#writing to text file
text_file1.write(classcode )
text_file1.write(' ')
text_file1.write(name )
text_file1.write(' ')

#converting score to string so that it can be written to txt file
scr = str(cp)
text_file1.write(scr )

#adding a new line after each students score
text_file1.write("\n")

#close the txt file after it has been used
text_file1.close()

#reading the txt file
text_file1 = open("data.dat", "r")
print(text_file1.read())
text_file1.close()



import operator

with open("data.dat") as f:
students = []
for line in f:
fields = line.split()
students.append( (fields[0], fields[1], int(fields[2])) )

# sort by the student's name
sortedByName = sorted(students, key=operator.itemgetter(1,2))

for r in sortedByName:
classcode, name, score = r
print("name:", name, "classcode:", classcode, "score:", score)
随机导入
q=0
ans=0
学生姓名=[]
升序=[]
classcode=输入(“输入您的类(1、2或3):”)
如果类代码=='1':
name=输入(“输入您的姓名:”)
n=int(输入(“输入您想要尝试的问题数量:”)
#将详细信息写入txt文件group1
text_file1=打开(“data.dat”、“a”)

当问题解决时。我通过使用-sortedByName=sorted(students,key=lambda x:(x[1],-x[2])实现了这种排序。唯一的问题是它似乎不喜欢用大写字母对名称进行排序