问答详情
源自:5-7 Python替换list中的元素

有没有更便捷的方法啊

names=['Alice', 'Bob', 'Candy', 'David', 'Ellena']

grade=[89, 72, 88, 79, 99]

for i in range(4):

    for j in range(i+1,5):

        if grade[j]>grade[i]:

            names[i],names[j]=names[j],names[i]

            grade[i],grade[j]=grade[j],grade[i]

print(names)


提问者:xir0905 2020-10-09 13:41

个回答

  • 慕标7081267
    2021-03-27 12:13:48


    name = ['Alice', 'Bob', 'Candy', 'David', 'Ellena']

    score = [89, 72, 88, 79, 99]

    scores = [89, 72, 88, 79, 99]

    score.sort(reverse = True)

    rank = []

    for i in range(len(score)):

        for j in range(len(score)):

            if score[i] == scores[j]:

                rank.append(name[j])

    print(rank)


  • Android精灵
    2020-10-10 11:02:15

    L = ['Alice','Bob','Candy','David','Ellena']

    L[0]='Ellena'

    L[1]='Alice'

    L[2]='Candy'

    L[3]='David'

    L[4]='Bob'

    print(L)