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

这个只能手动排序吗 那如果人数很多要怎么操作

问题描述:
有主动排序的方法吗

提问者:阿差 2021-07-31 15:33

个回答

  • AlvinCN
    2023-04-23 02:40:17

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

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

    sort = sorted(zip(students,score),key=lambda x: x[1],reverse=True)

    print(sort)

    print(zip(*sort)[0])


  • 芬芳莹莹
    2022-01-05 15:43:49

    自己写代码:

    结果:

    [99, 89, 88, 79, 72]
    ['Ellena', 'Alice', 'Candy', 'David', 'Bob']

    代码:

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

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

    for i in range(len(scores)):

        s = scores[i]

        flag = i

        for j in range(i,len(scores)):

            if(scores[flag] < scores[j]):

                flag = j

        temp = ''

        x = 0

        temp = names[i]

        names[i] = names[flag]

        names[flag] = temp

        x = scores[i]

        scores[i] = scores[flag]

        scores[flag] = x

    print(scores)   

    print(names)

    下载视频          

  • weixin_慕哥1338196
    2021-08-02 15:38:23

    # Enter a code

    def get_second(elem):

        return elem[1]


    L = [('Alice',89), ('Bob',72), ('Candy',88), ('David',79), ('Ellena',99)]

    L.sort(key=get_second,reverse=True)

    print(L)


  • weixin_慕哥1338196
    2021-08-02 15:38:07

    # Enter a code

    def get_second(elem):

        return elem[1]


    L = [('Alice',89), ('Bob',72), ('Candy',88), ('David',79), ('Ellena',99)]

    L.sort(key=get_second,reverse=True)

    print(L)