我正在努力计算体育联盟的排名。领带断路器基于头2记录。因此,为了确定在积分达到平局时排名更高的球队,我需要能够从一个字典中拉开团队之间的头部记录。
我的计划是为每个团队创建一个字典,其中包含针对其他团队的所有头部2个头部记录。
问题是,团队名称每年都会发生变化(以及团队数量)。所以我需要遍历团队列表(从数据库中调用)并为每个团队创建字典。这可能吗?有没有更简单的方法来解决这个问题?因为我可能不知道我在做什么。
我的代码目前看起来像这样:
wins = collections.defaultdict(int)
losses = collections.defaultdict(int)
ties = collections.defaultdict(int)
points = collections.defaultdict(int)
games_rem = collections.defaultdict(int)
poss_points = collections.defaultdict(int)
for team in team_dict:
wins[team.shortname] = team.wins
losses[team.shortname] = team.losses
ties[team.shortname] = team.ties
points[team.shortname] = team.points
games_rem[team.shortname] = mens_games.count + team.wins + team.losses + team.ties
poss_points[team.shortname] = team.points + games_rem[team.shortname] * 2
for team in team_dict:
for other_teams in team_dict:
if other_teams.shortname == team.shortname:
continue
vs_"string{0}".format(other_teams.shortname) = collections.defaultdict(int)
for game in game_dict:
if game.team1 == other_teams.shortname and game.team2 == team.shortname and game.team1score < game.team2score:
vs_"string{0}".format(other_teams.shortname)[team.shortname] += 1
if game.team1 == other_teams.shortname and game.team2 == team.shortname and game.team1score > game.team2score:
vs_"string{0}".format(other_teams.shortname)[team.shortname] -= 1
if game.team2 == other_teams.shortname and game.team1 == team.shortname and game.team2score < game.team1score:
vs_"string{0}".format(other_teams.shortname)[team.shortname] += 1
if game.team2 == other_teams.shortname and game.team1 == team.shortname and game.team2score > game.team1score:
vs_"string{0}".format(other_teams.shortname)[team.shortname] -= 1
问题显然是我尝试根据团队名称分配dictonaries
vs_"string{0}".format(other_teams.shortname) = collections.defaultdict(int)
我认为这绝对是不允许的,但我无法想出一个更好的方法来解决这个问题。有什么想法吗?
编辑: 为不良解释道歉。 我试图输出的是每个团队的单独字典,如下所示:
vs_teamA = {'teamB': 1, 'teamC': 1, 'teamD': -1, 'teamE': 0}
vs_teamB = {'teamA': -1, 'teamC': 1, 'teamD': -1, 'teamE': 0}
vs_teamC = {'teamA': -1, 'teamB': -1, 'teamD': -1, 'teamE': 0}
vs_teamD = {'teamA': 1, 'teamB': 1, 'teamC': 1, 'teamE': 0}
vs_teamE = {'teamA': 0, 'teamB': 0, 'teamC': 0, 'teamD': 0}
如果一支球队与另一支球队有胜利记录,那么这个数字将是正数,如果他们有失败的记录则为负数。
答案 0 :(得分:0)
这里确实没有具体的问题,但我认为你可以从中学习python模块pandas
。作为一个小例子,看看如何使用“类字典”界面组织数据:
import pandas as pd
stat_names = "points", "games_rem", "poss_points"
team_names = "A", "B", "C"
df = pd.DataFrame(0, index=stat_names, columns=team_names)
df["A"]["points"] = 30
df["B"]["points"] = 20
df["C"]["poss_points"] = 2
print df
哪个给出了
A B C
points 30 20 0
games_rem 0 0 0
poss_points 0 0 2
答案 1 :(得分:0)
为什么你不能将字典视为价值?
{'vs_teamA': {'teamB': 1, 'teamC': 1, 'teamD': -1, 'teamE': 0},
'vs_teamB': {'teamA': -1, 'teamC': 1, 'teamD': -1, 'teamE': 0},
'vs_teamC': {'teamA': -1, 'teamB': -1, 'teamD': -1, 'teamE': 0},
'vs_teamD': {'teamA': 1, 'teamB': 1, 'teamC': 1, 'teamE': 0},
'vs_teamE': {'teamA': 0, 'teamB': 0, 'teamC': 0, 'teamD': 0}}
答案 2 :(得分:0)
我建议您使用嵌套字典
In [371]: from collections import defaultdict
In [380]: match_results = defaultdict(lambda: defaultdict(int))
In [381]: match_results['vs_teamA']['teamB']+=1
In [382]: match_results['vs_teamA']['teamC']+=-1
In [383]: match_results['vs_teamC']['teamB']+=-1
In [386]: for team, games in match_results.iteritems():
.....: print team
.....: print games
vs_teamA
defaultdict(<type 'int'>, {'teamB': 1, 'teamC': -1})
vs_teamC
defaultdict(<type 'int'>, {'teamB': -1})