python - Weird sum coming out of complex dictionary -
python - Weird sum coming out of complex dictionary -
having problem python code (dealing info midi files if you're curious variable names).
song.getintervals()
matrix of assorted integers, e.g.:
[[-2,0,0,5,3],[2],[4,3,0]]
except much longer.
chrom
defined as:
def createintfunc(a,b): temp1={} temp2={} x in range(a,b): temp1[x]=0 x in temp1: temp2[x]=dict(temp1) homecoming temp2 chrom=createintfunc(-17,18)
so it's dictionary keys integers , values dictionaries have integers both keys , values.
here piece of code of concern:
for phrase in song.getintervals(): noteind in range(1,len(phrase)): chrom[phrase[noteind-1]][phrase[noteind]]+=(1/float(sum([len(a) in song.getintervals()]))) print sum([sum(a) in [b.values() b in chrom.values()]])
which prints value that's close 0.8 depending on song.getintervals() is. don't see how wouldn't homecoming 1. i'm repeatedly adding value starts @ 0 fraction that's defined inverse of many times i'm going add together it. should 0+(1/x)*x=1.
what not seeing?
here problem:
your value of "1/x" not getting added "x" times, because within of loop not running "x" times. example, when length of phrase 1, "1/x" not added dictionary, because inner loop runs on indices satisfy inequality 1 <= noteind < len(phrase). when len(phrase)==1, no index can satisfy inequality, within of loop doesn't execute. i'm not sure intention do, i'm not sure how prepare without knowing intended behavior, that's why it's performing way is.
python dictionary
Comments
Post a Comment