i'm trying automate series of tests, , need have loop change parameters.
mydictionary={'a':10,'b':100,'c':30} def swaprules(d,rule): "clear dict, set 100 rule match 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 dict ''' in d.keys(): d[i]=0 return d def tests(d): itertools import starmap, repeat, izip keys=d.keys() paramsdictionaries=list(starmap(swaprules, izip(repeat(d),keys))) print(paramsdictionaries)
i'cannot understand why when run test(mydictionary) output contains same value. seems issue not in wrong use of itertools: repl shows substituting simple list comprehension:
in [9]: keys=mydictionary.keys() in [10]: [tr.swaprules(mydictionary,jj) jj in keys] {'a': 0, 'c': 0, 'b': 100} {'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}]
i'm puzzled since when swaprules function evoked alone, produces expected result, shown print statements... idea on i'm doing wrong? chance caching something?
answering own question since i've found work:
def swaprules2(d,rule): '''clear dict, return new rule set''' keys=d.keys() if rule not in keys: raise exception("wrong key") outd=dict.fromkeys(keys,0) outd[rule]=100 return outd
and in list comprehension returns expected output:
in [16]: [tr.swaprules2(mydict,jj) jj in keys] out[16]: [{'a': 100, 'b': 0, 'c': 0}, {'a': 0, 'b': 0, 'c': 100}, {'a': 0, 'b': 100, 'c': 0}]
however, still not understand why previous approach did not.
Comments
Post a Comment