您有几种选择可以遍历字典。
如果迭代字典本身(
for team inleague),则将迭代字典的键。当使用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)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)