【发布时间】:2015-08-16 23:22:15
【问题描述】:
我的脚本返回到该程序中的第一个 for 循环时遇到问题,现在它只返回到第二个。我试图弄清楚如何强制它完成所有步骤。
提前致谢
import copy
test = {'AEP': {'days': [1]}, 'AES': {'days': [32]},'AIS': {'days': [7]},'TWO': {'days': [35]}}
print (test)
for stock in test:
a = stock
print ("step one")
for stock in test.itervalues():
print ("step two")
for days in stock.itervalues():
print ("step three")
for i in days:
print ("step four")
if i <= 30:
days[days.index(i)] = i + 1
else:
test_2 = copy.deepcopy(test)
test_2.pop(a)
print (str(a) + " Has been removed")
test = copy.deepcopy(test_2)
print (test)
当前流/日志:
{'AEP': {'days': [1]}, 'AES': {'days': [32]}, 'TWO': {'days': [35]}, 'AIS': {'days': [7]}}
step one
step two
step three
step four
step two
step three
step four
AEP Has been removed
step two
step three
step four
Traceback (most recent call last):
File "fortest.py", line 19, in <module>
test_2.pop(a)
这是我希望看到的最终结果
test = {'AEP': {'days': [1]},'AIS': {'days': [7]}}
【问题讨论】:
-
我还不确定发生了什么,但你想做什么?只过滤掉包含
dict的键,其'days'键包含一个包含大于30 的值的列表? -
很遗憾,您当前的代码存在几个问题。你可以尝试修复它们,但如果我理解你的目标,有更好的方法来做到这一点。你能澄清一下你想要做什么吗?
-
这是一个更大的脚本的一部分,但这是目标。我正在努力做到这一点,所以当我的股票出售时,我不会再购买 31 天。当股票被购买时,它被添加为一个值为 0 的嵌套字典。每天开市时它会增加一个,一旦达到 30,它将从不买字典中删除。
-
但我说的是你如何过滤
dict,对吧? -
@cyphase 在你回复之前我还没有完成评论,但这不是我想要的操作方式。
标签: python for-loop dictionary scope copy