阿差
2021-07-31 15:33
问题描述:
有主动排序的方法吗
自己写代码:
结果:
[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)
Python3 入门教程(新版)
154173 学习 · 1075 问题
相似问题