【问题标题】:Iterating Over Dictionary Key Values Corresponding to List in Python迭代与Python中的列表对应的字典键值
【发布时间】:2011-11-16 13:38:26
【问题描述】:

在 Python 2.7 中工作。我有一个字典,其中团队名称作为键,每个团队的得分和允许的运行量作为值列表:

NL_East = {'Phillies': [645, 469], 'Braves': [599, 548], 'Mets': [653, 672]}

我希望能够将字典输入一个函数并遍历每个团队(键)。

这是我正在使用的代码。现在,我只能一个队一个队地去。我将如何迭代每个团队并打印每个团队的预期 win_percentage?

def Pythag(league):
    runs_scored = float(league['Phillies'][0])
    runs_allowed = float(league['Phillies'][1])
    win_percentage = round((runs_scored**2)/((runs_scored**2)+(runs_allowed**2))*1000)
    print win_percentage

感谢您的帮助。

【问题讨论】:

    标签: python list function dictionary loops


    【解决方案1】:

    您有多个选项可用于遍历字典。

    如果您遍历字典本身 (for team in league),您将遍历字典的键。使用 for 循环进行循环时,无论您循环遍历 dict (league) 本身还是 league.keys(),其行为都是相同的:

    for team in league.keys():
        runs_scored, runs_allowed = map(float, league[team])
    

    您还可以通过迭代 league.items() 来同时迭代键和值:

    for team, runs in league.items():
        runs_scored, runs_allowed = map(float, runs)
    

    您甚至可以在迭代时执行元组解包:

    for team, (runs_scored, runs_allowed) in league.items():
        runs_scored = float(runs_scored)
        runs_allowed = float(runs_allowed)
    

    【讨论】:

    • dict.iteritems() 自 Python3 以来已被删除。你应该使用 dict.items() 代替
    • dict.iterkeys() 在 Python 3 中也被删除了。你应该使用 dict.keys() 来代替
    • dict.itervalues() 在 Python 3 中也被删除了。你应该使用 dict.values() 来代替
    • 如何检查 if 语句的 dict 中是否有可用的键?是不是也一样?
    【解决方案2】:

    您也可以非常轻松地遍历字典:

    for team, scores in NL_East.iteritems():
        runs_scored = float(scores[0])
        runs_allowed = float(scores[1])
        win_percentage = round((runs_scored**2)/((runs_scored**2)+(runs_allowed**2))*1000)
        print '%s: %.1f%%' % (team, win_percentage)
    

    【讨论】:

    • @BurtonGuster:当您认为某个答案值得时,请投票(点击帖子左侧的“向上”按钮)!这样你也可以帮助社区!
    【解决方案3】:

    字典有一个名为 iterkeys() 的内置函数。

    试试:

    for team in league.iterkeys():
        runs_scored = float(league[team][0])
        runs_allowed = float(league[team][1])
        win_percentage = round((runs_scored**2)/((runs_scored**2)+(runs_allowed**2))*1000)
        print win_percentage
    

    【讨论】:

      【解决方案4】:

      字典对象允许您迭代它们的项目。此外,通过模式匹配和__future__ 的划分,您可以稍微简化一下。

      最后,您可以将逻辑与打印分开,以便以后更容易重构/调试。

      from __future__ import division
      
      def Pythag(league):
          def win_percentages():
              for team, (runs_scored, runs_allowed) in league.iteritems():
                  win_percentage = round((runs_scored**2) / ((runs_scored**2)+(runs_allowed**2))*1000)
                  yield win_percentage
      
          for win_percentage in win_percentages():
              print win_percentage
      

      【讨论】:

        【解决方案5】:

        列表推导可以缩短事情的时间...

        win_percentages = [m**2.0 / (m**2.0 + n**2.0) * 100 for m, n in [a[i] for i in NL_East]]
        

        【讨论】:

          猜你喜欢
          • 2015-09-28
          • 1970-01-01
          • 2018-09-21
          • 2014-09-26
          • 1970-01-01
          • 1970-01-01
          • 2012-09-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多