我开始阅读 python 文档。我最终得到了这样的声明:
在迭代同一集合时修改集合的代码可能很难正确处理。相反,循环遍历集合的副本或创建新集合通常更直接:
# Strategy: Iterate over a copy
for user, status in users.copy().items():
if status == 'inactive':
del users[user]
# Strategy: Create a new collection
active_users = {}
for user, status in users.items():
if status == 'active':
active_users[user] = status
我无法理解解决方案。这段代码是如何工作的?我的意思是我想到了使用一个变量来迭代一个列表,但是使用两个变量很难理解。来源:https ://docs.python.org/3/tutorial/controlflow.html#for-statements
精慕HU
大话西游666
幕布斯6054654
交互式爱情
随时随地看视频慕课网APP
相关分类