在 For 循环 Python 中从迭代总数中减去字典值

发布于 2024-12-05 07:17:53 字数 867 浏览 2 评论 0原文

使用 Python 2.7 工作。

我有一个字典,其中团队名称作为键,值包含在列表中。第一个值是团队得分的跑动次数,第二个是允许的跑动次数:

NL = {'Phillies': [662, 476], 'Braves': [610, 550], 'Mets': [656, 687]}

我有一个函数可以迭代每个键并提供整个字典的得分和允许的总跑动次数。我还希望能够从总数中减去每个单独的球队,并创建一个联盟减去球队价值。

我首先尝试了类似的操作:

def Tlog5(league, league_code):
    total_runs_scored = 0
    total_runs_allowed = 0
    for team, scores in league.iteritems():
        total_runs_scored += float(scores[0])
        total_runs_allowed += float(scores[1])
        team_removed_runs = total_runs_scored - scores[0]

不幸的是,这似乎只从已经迭代的值中减去,而不是从完整的总数中减去。因此,对于字典中的第一支球队, team_removed_runs 为 0,对于第二支球队,它是前两支球队的总跑动次数减去第二支球队的总跑动次数(只留下第一支球队的总跑动次数)。

我尝试移动 team_removed_runs = Total_runs_scored - 分数[0]超出了for循环,但后来我只得到了字典中最后一支球队的值,

有没有办法可以返回所有球队的team_removed运行 。在字典中吗?

谢谢您的帮助!

Working in Python 2.7.

I have a dictionary with team names as the keys and the values are contained in lists. The first value is the amount of runs the team scored, the second is runs allowed:

NL = {'Phillies': [662, 476], 'Braves': [610, 550], 'Mets': [656, 687]}

I have a function that iterates over each key and provides the total runs scored and allowed for the dictionary as a whole. I would also like to be able to subtract each individual team from the total, and create a league minus team value.

I first tried something along the lines of this:

def Tlog5(league, league_code):
    total_runs_scored = 0
    total_runs_allowed = 0
    for team, scores in league.iteritems():
        total_runs_scored += float(scores[0])
        total_runs_allowed += float(scores[1])
        team_removed_runs = total_runs_scored - scores[0]

Unfortunately, that appeared to be subtracting from only the values that had already been iterated instead of the complete total. So, for the first team in the dictionary, team_removed_runs was 0, for the second team it was the the total runs for of the first two teams minus the second teams total (leaving only the first teams total.

I tried to move the team_removed_runs = total_runs_scored - scores[0] out of the for loop, but then I only got a value for the last team in the dictionary.

Is there a way I can return the team_removed runs for all the teams in the dictionary?

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

左耳近心 2024-12-12 07:17:53

如果您想要字典中每个团队的team_removed_runs,则需要返回字典并计算总跑次数减去每个团队的跑次数。像这样的东西

team_removed_runs = {}
for team, scores in league.iteritems():
    team_removed_runs[team] = [total_runs_scored - scores[0],
                               total_runs_allowed - scores[1]]

将使用整个联赛的 total_runs_scoredtotal_runs_allowed 的最终值,然后从该总数中减去每个球队的值,将结果存储在字典中 team_removed_runs。因此,如果您想要联盟总数减去费城人队的价值,您可以在

team_removed_runs['Phillies']

If you want the team_removed_runs for every team in the dictionary, you'll need to go back through the dictionary and compute the total number of runs minus the runs for each team. Something like

team_removed_runs = {}
for team, scores in league.iteritems():
    team_removed_runs[team] = [total_runs_scored - scores[0],
                               total_runs_allowed - scores[1]]

This will use the final values of total_runs_scored and total_runs_allowed for the entire league, and then subtract each teams values from that total, storing the result in the dictionary team_removed_runs. So if you wanted the value for the league total less the Phillies, you could find this at

team_removed_runs['Phillies']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文