0
我试图自动化一系列的测试,我需要有一个循环,我改变参数。生成模板字典和键字典列表
mydictionary={'a':10,'b':100,'c':30}
def swapRules(d,rule):
"clear dict, set to 100 the rule that match the string"
print d, rule
if not d.has_key(rule): raise Exception("wrong string")
d=resetDict(d)
d[rule]=100
return d
def resetDict(d):
'''clear the dict '''
for i in d.keys():
d[i]=0
return d
def tests(d):
from itertools import starmap, repeat, izip
keys=d.keys()
paramsDictionaries=list(starmap(swapRules, izip(repeat(d),keys)))
print(paramsDictionaries)
我不明白为什么当我运行test(mydictionary)时,输出始终包含相同的值。看来,这个问题是不是在错误使用迭代工具的:作为REPL显示了一个简单的列表理解代:
In [9]: keys=mydictionary.keys()
In [10]: [tr.swapRules(mydictionary,jj) for jj in keys]
{'a': 0, 'c': 0, 'b': 100} a
{'a': 100, 'c': 0, 'b': 0} c
{'a': 0, 'c': 100, 'b': 0} b
Out[10]:
[{'a': 0, 'b': 100, 'c': 0},
{'a': 0, 'b': 100, 'c': 0},
{'a': 0, 'b': 100, 'c': 0}]
我当swapRules功能单独诱发因为实在不明白,产生预期结果,如打印声明所显示的...对我在做什么错误有任何想法?有没有可能缓存一些东西?
应该发生什么? – user2357112
我认为: {{'a':100,'b':0,'c':0}, {'a':0,'b':0,'c':100}, { 'a':0,'b':100,'c':0}] – deddu