Hi
I have written the following which works, but I would like to write it
less clumsy. I have a dictionary in which I loop through the keys for
a dynamic programming algorithm. If a key is present I test if its
value is better than the current, if it is not present I just create
it. Would it be possible to write it more compact?
############### 3
c=1
s=8
x=2
l=list()
l.append(dict() )
l[0][5]=0
l.append(dict() )
for a, e in l[-2].iteritems():
if a+c<s: ##### Can this improved?
if a+c in l[-1]: #####
if l[-1][a+c]<x+e: #####
l[-1][a+c]=x+e #####
else: #####
l[-1][a+c]=x+e #####
print l
############### ######
tia,
--
Brian (remove the sport for mail)
Rugbyklubben Speed Scandinavian Open 7s Rugby http://www.rkspeed.dk
I have written the following which works, but I would like to write it
less clumsy. I have a dictionary in which I loop through the keys for
a dynamic programming algorithm. If a key is present I test if its
value is better than the current, if it is not present I just create
it. Would it be possible to write it more compact?
############### 3
c=1
s=8
x=2
l=list()
l.append(dict() )
l[0][5]=0
l.append(dict() )
for a, e in l[-2].iteritems():
if a+c<s: ##### Can this improved?
if a+c in l[-1]: #####
if l[-1][a+c]<x+e: #####
l[-1][a+c]=x+e #####
else: #####
l[-1][a+c]=x+e #####
print l
############### ######
tia,
--
Brian (remove the sport for mail)
Rugbyklubben Speed Scandinavian Open 7s Rugby http://www.rkspeed.dk
Comment