【发布时间】:2015-01-19 11:16:21
【问题描述】:
我刚刚开始学习一点 Python。我正在使用一些基本的词典。我有一本字典,我复印了。然后我拿另一本字典并从我的副本中取出值:
teams_goals = {'chelsea' : 2, 'man u' : 3,'Arsenal':1,'Man city':2}
print teams_goals
test_teams = {'chelsea' : 1, 'man u' : 1,'Arsenal':1}
teams_goals_copy = teams_goals.copy()
for team in test_teams:
for j in range(test_teams[team]):
teams_goals_copy[team]= teams_goals_copy[team]- 1
print teams_goals_copy
这给我留下了一本包含一些零值的字典。我想要的是一种在它们等于零时从字典中删除项目的方法。
我找到了this previous thread here;似乎这曾经在以前的版本上工作过,但我不明白解决方法。
【问题讨论】:
-
您遇到的具体问题是什么?您想遍历字典,删除值等于 0 的键;你在哪里尝试实现它,它出了什么问题?
标签: python dictionary