Python 3.4 - 根据两组数据返回排名(只有当另一组产生平局时才使用一组)

Python 3.4 - returning rankings based on two sets of data (with one only used when the other produces a tie)

我正在 Python 3.4.3 中的一个联赛 table 程序,其中有四支球队,我需要根据总积分和客场积分为他们生成排名,其中总积分多的球队获胜,客场积分用于结算相等的总积分数(客场积分越多越好)。我有所有这些值的变量,我只需要以某种方式获得它们的排名。

这是我目前拥有的:

# 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 代表最高分。 我不是编程方面的专家,所以请保持解释相当简单。

非常感谢您的帮助,
barnstorm3r

(是的,我知道列表对于存储这些值会更有效,但其余代码(将近 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)