问题:迭代对应于Python中列表的字典键值
使用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
谢谢你的帮助。
Working in Python 2.7. I have a dictionary with team names as the keys and the amount of runs scored and allowed for each team as the value list:
NL_East = {'Phillies': [645, 469], 'Braves': [599, 548], 'Mets': [653, 672]}
I would like to be able to feed the dictionary into a function and iterate over each team (the keys).
Here’s the code I’m using. Right now, I can only go team by team. How would I iterate over each team and print the expected win_percentage for each team?
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
Thanks for any help.
回答 0
您有几种选择可以遍历字典。
如果迭代字典本身(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)
You have several options for iterating over a dictionary.
If you iterate over the dictionary itself (for team in league
), you will be iterating over the keys of the dictionary. When looping with a for loop, the behavior will be the same whether you loop over the dict (league
) itself, or league.keys()
:
for team in league.keys():
runs_scored, runs_allowed = map(float, league[team])
You can also iterate over both the keys and the values at once by iterating over league.items()
:
for team, runs in league.items():
runs_scored, runs_allowed = map(float, runs)
You can even perform your tuple unpacking while iterating:
for team, (runs_scored, runs_allowed) in league.items():
runs_scored = float(runs_scored)
runs_allowed = float(runs_allowed)
回答 1
您也可以很容易地遍历字典:
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)
You can very easily iterate over dictionaries, too:
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)
回答 2
字典具有一个称为的内置函数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
Dictionaries have a built in function called iterkeys()
.
Try:
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
回答 3
字典对象允许您迭代其项目。此外,通过模式匹配和__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
Dictionary objects allow you to iterate over their items. Also, with pattern matching and the division from __future__
you can do simplify things a bit.
Finally, you can separate your logic from your printing to make things a bit easier to refactor/debug later.
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
回答 4
列表理解可以缩短内容…
win_percentages = [m**2.0 / (m**2.0 + n**2.0) * 100 for m, n in [a[i] for i in NL_East]]
List comprehension can shorten things…
win_percentages = [m**2.0 / (m**2.0 + n**2.0) * 100 for m, n in [a[i] for i in NL_East]]