Python 3.4-基于两组数据返回排名(其中一组仅在另一组产生平局时使用)
我正在Python 3.4.3中开发一个排行榜程序,其中有四个团队,我需要根据总分和客场得分为他们生成排名,总分最多的团队获胜,客场得分用于解决相等的总分计数(客场得分越多越好)。我有所有这些值的变量,我只需要以某种方式获得它们的排名 这就是我目前的情况:Python 3.4-基于两组数据返回排名(其中一组仅在另一组产生平局时使用),python,sorting,ranking,Python,Sorting,Ranking,我正在Python 3.4.3中开发一个排行榜程序,其中有四个团队,我需要根据总分和客场得分为他们生成排名,总分最多的团队获胜,客场得分用于解决相等的总分计数(客场得分越多越好)。我有所有这些值的变量,我只需要以某种方式获得它们的排名 这就是我目前的情况: # loading points from file pointsfile = open("points.txt", "r+") pointsfile.seek(0, 0) apoints = str(int(pointsfile.read(
# loading points from file
pointsfile = open("points.txt", "r+")
pointsfile.seek(0, 0)
apoints = str(int(pointsfile.read(2)))
pointsfile.seek(2, 0)
bpoints = str(int(pointsfile.read(2)))
pointsfile.seek(4, 0)
cpoints = str(int(pointsfile.read(2)))
pointsfile.seek(6, 0)
dpoints = str(int(pointsfile.read(2)))
pointsfile.close()
# loading away points from file
awayfilea = open("awaypointsa.txt", "r+")
awayfileb = open("awaypointsb.txt", "r+")
awayfilec = open("awaypointsc.txt", "r+")
awayfiled = open("awaypointsd.txt", "r+")
aaway = awayfilea.read()
baway = awayfileb.read()
caway = awayfilec.read()
daway = awayfiled.read()
awayfilea.close()
awayfileb.close()
awayfilec.close()
awayfiled.close()
# attempting to sort using a dictionary (works assuming no teams have equal points)
pointsdict = {"a": apoints, "b": bpoints, "c": cpoints, "d": dpoints}
pointsdictsorted = sorted(pointsdict, key=pointsdict.__getitem__, reverse=True)
print(pointsdictsorted)
我想要以下格式的结果:
arank = [value]
brank = [value]
crank = [value]
drank = [value]
其中,[value]是从1到4的数字,其中1表示最高点数。
我不是编程方面的专家,所以请保持解释相当简单
非常感谢您的帮助,谷仓风暴3R (是的,我知道列表存储这样的值会更有效,但代码的其余部分(近600行)是为单个变量设置的。)
pointsdict = {"a": apoints, "b": bpoints, "c": cpoints, "d": dpoints}
pointsdictsorted = sorted(pointsdict, key=pointsdict.__getitem__, reverse=True)
print(pointsdictsorted)
# Take the sorted list of values, and create a tuple for each: (value, rank)
for idx, elem in enumerate(pointsdictsorted):
pointsdictsorted[idx] = (elem, idx + 1)
# Resort for teams, not scores, and assign.
ranks = sorted(pointsdictsorted)
arank = ranks[0][1]
brank = ranks[1][1]
crank = ranks[2][1]
drank = ranks[3][1]
print(arank, brank, crank, drank)