问题描述:
有主动排序的方法吗
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])
自己写代码:
结果:
[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)
# 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)
# 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)