Python 从比赛列表中获取分数
我想从这样的列表中写一个脚本Python 从比赛列表中获取分数,python,list,Python,List,我想从这样的列表中写一个脚本 Matches=[ ("Team Name1", 120, "Team Name2", 56 ), ... ,] for match in matches: score=0 if match[1] > match[3]: score + = 2 res=[match[0],score] 这是一个比赛列表,有两个参赛队的名字和他们的结果,给我一个列表作为输出,每个队的名字和分数(每赢2分)。我的输出应该如下
Matches=[ ("Team Name1", 120, "Team Name2", 56 ), ... ,]
for match in matches:
score=0
if match[1] > match[3]:
score + = 2
res=[match[0],score]
这是一个比赛列表,有两个参赛队的名字和他们的结果,给我一个列表作为输出,每个队的名字和分数(每赢2分)。我的输出应该如下所示:
Team Score
Team Name 1 26
Team Name 2 30
...
我已经达到了这样的程度
Matches=[ ("Team Name1", 120, "Team Name2", 56 ), ... ,]
for match in matches:
score=0
if match[1] > match[3]:
score + = 2
res=[match[0],score]
还有一点是,在比赛列表中,每个队不能只打一场。希望这会有所帮助
matches=[ ("A", 120, "B", 56 ), ("A", 120, "C", 56 ), ("B", 120, "C", 56 )]
TEAM={}
for match in matches:
TEAM[match[0]]=0
TEAM[match[2]]=0
for match in matches:
if match[1] > match[3]:
TEAM[match[0]]+=2
elif match[3] > match[1]:
TEAM[match[2]]+=2
for match in TEAM:
print match, ":", TEAM[match]
我的建议并不是作为一个答案,它只是一个建议,被接受的答案也可以这样写:
matches=[ ("A", 120, "B", 56 ), ("A", 120, "C", 56 ), ("B", 120, "C", 56 )]
TEAM={}
for match in matches:
if not match[0] in TEAM:
TEAM[match[0]] = 0
if not match[2] in TEAM:
TEAM[match[2]] = 0
if match[1] > match[3]:
TEAM[match[0]] += 2
elif match[3] > match[1]:
TEAM[match[2]] += 2
for match in TEAM:
print(match, ":", TEAM[match])
这不是一个重大变化,只是一个替代方案。什么是输入,什么是输出?你的问题不清楚。比赛名单是我的输入,这正是我要找的,谢谢你!!!!