我正在做一个对奖牌表进行排序的函数。例如:如果两个或更多的队伍拥有相同数量的金牌,则从最多到最少对银牌进行评判,然后对铜牌进行评判。
名单中的每一个国家都是金、银、铜。
[[1, 2, 0], [0, 1, 0], [2, 0, 0], [0, 0, 3]]
这就是返回:
[[2, 0, 0], [1, 2, 0], [0, 1, 0], [0, 0, 3]]
我的函数正在工作,但我找不到一种方法来停止递归...
代码:
def ordem(lista,lista2,x):
cond=0
cond2=0
cond3=0
cont2=0
for xx in lista:
if x-cont2==1:
break
if lista[cont2+1][0] > lista[cont2][0]:
lista[cont2+1],lista[cont2]=lista[cont2],lista[cont2+1]
lista2[cont2+1],lista2[cont2]=lista2[cont2],lista2[cont2+1]
cond=1
cond2=1
#print("1")
#cont2+=1
if lista[cont2+1][1] > lista[cont2][1] and cond2 ==0:
lista[cont2+1],lista[cont2]=lista[cont2],lista[cont2+1]
lista2[cont2+1],lista2[cont2]=lista2[cont2],lista2[cont2+1]
cond=1
cond3=1
#print("2")
#cont2+=1
if lista[cont2+1][2] > lista[cont2][2] and (cond2==0 and cond3==0):
lista[cont2+1],lista[cont2]=lista[cont2],lista[cont2+1]
lista2[cont2+1],lista2[cont2]=lista2[cont2],lista2[cont2+1]
cond=1
#print("3")
#cont2+=1
#else: cond=False
cont2+=1
if cond!=1:
#print(lista)
return lista2
#print(lista)
#print("cond:"+str(cond))
return ordem(lista,lista2,x)
我尝试将已经排序到列表中的元素添加到列表中,然后检查它们在进行切换时是否在列表中,但同样不起作用
转载请注明出处:http://www.sywsjj.net/article/20230330/1404133.html