迭代对应于Python中列表的字典键值

迭代对应于Python中列表的字典键值,第1张

迭代对应于Python中列表的字典键值

您有几种选择可以遍历字典。

如果迭代字典本身(

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)


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5653775.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存