我对我解决这个问题的方法有了第二个想法。我是python的新手。所以还有很多东西可以覆盖。 https://www.testdome.com/for-developers/solve-question/11195 我现在坚持这个问题。我有点暂时的解决方案。我想知道是否有更优化的方法。
from collections import Counter
from collections import OrderedDict
class LeagueTable:
def __init__(self, players):
self.standings = OrderedDict([(player, Counter()) for player in players])
def record_result(self, player, score):
self.standings[player]['games_played'] += 1
self.standings[player]['score'] += int(score)# print(self.standings)# print("")
def player_rank(self, rank): #res = OrderedDict()
x=self.standings
d=self.standings
def swap_list(xl,co):
xl[co],xl[co+3]=xl[co+3],xl[co]
xl[co+1],xl[co+4]=xl[co+4],xl[co+1]
xl[co+2],xl[co+5]=xl[co+5],xl[co+2]
return(xl,co)
player_list=[]
lc=len(x)
lc1=lc
lc=lc*3
for p,c in x.items():
player_list.append(p)
player_list.append(c["score"])
player_list.append(c["games_played"])
for j in range(lc1):
for i in range(0,lc-3,3):
# print(i+4)
if (player_list[i+1] < player_list[i+4]):
swap_list(player_list,i)
elif (player_list[i+1]==player_list[i+4]):
if(player_list[i+2]>player_list[i+5]):
swap_list(player_list,i)
elif(player_list[i+2]==player_list[i+5]):
pass
return(player_list[(rank-1)*3])
table = LeagueTable(['Mike', 'Chris', 'Arnold'])
table.record_result('Mike', 2)
table.record_result('Mike', 3)
table.record_result('Arnold', 5)
table.record_result('Chris', 5)
rk=int(input("enter rank \n"))
print(table.player_rank(rk))
任何帮助将不胜感激