473,320 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Dictionary question

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)
http://www.et.web.mek.dtu.dk/Staff/be/be.html
Rugbyklubben Speed Scandinavian Open 7s Rugby http://www.rkspeed.dk
Jul 18 '06 #1
7 1293
Brian Elmegaard <br***@rkspeed-rugby.dkwrites:

At least it was clumsy to post a wrong example.
This shows what = find clumsy.

c=1
x=2

l=list()
l.append(dict())
l[0][5]=0

l.append(dict())

for a, e in l[-2].iteritems():
######### Can this be written better?
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

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)
http://www.et.web.mek.dtu.dk/Staff/be/be.html
Rugbyklubben Speed Scandinavian Open 7s Rugby http://www.rkspeed.dk
--
Brian (remove the sport for mail)
http://www.et.web.mek.dtu.dk/Staff/be/be.html
Rugbyklubben Speed Scandinavian Open 7s Rugby http://www.rkspeed.dk
Jul 18 '06 #2
Brian,
You can try the setdefault method of the dictionary.
For a dictionary D the setdefault work like this:
D.setdefault(k, defvalue). If k not in D then D[k] is set to defvalue
and defvalue is returned.
For example:
In [1]: d={}
In [2]: d.setdefault(1,5)
Out[2]:5
In [3]: d
Out[3]:{1: 5}
In your case you could do something like:

if l[-1].setdefault(a+c, x+e)<x+e:
l[-1][a+c]=x+e

If a+c not in l[-1] then it the if ... line will set it to x+e and x+e
will be returned, obviously the if ... will fail and the execution will
go on after the if block. If a+c is in l[-1] then if will compare
l[-1][a+c] to x+e.

Also for clarity you might actually want to assign some variables to
a+c and x+e since you repeat them so often. That actually might speed
up your code a little too, but don't do it just for a minor speedup do
it for clarity and simplicity most of all!

Good luck,
Nick Vatamaniuc
Brian Elmegaard wrote:
Brian Elmegaard <br***@rkspeed-rugby.dkwrites:

At least it was clumsy to post a wrong example.
This shows what = find clumsy.

c=1
x=2

l=list()
l.append(dict())
l[0][5]=0

l.append(dict())

for a, e in l[-2].iteritems():
######### Can this be written better?
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

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)
http://www.et.web.mek.dtu.dk/Staff/be/be.html
Rugbyklubben Speed Scandinavian Open 7s Rugby http://www.rkspeed.dk

--
Brian (remove the sport for mail)
http://www.et.web.mek.dtu.dk/Staff/be/be.html
Rugbyklubben Speed Scandinavian Open 7s Rugby http://www.rkspeed.dk
Jul 18 '06 #3
Brian Elmegaard wrote:
for a, e in l[-2].iteritems():
######### Can this be written better?
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
#########
I'd start with something like

for a, e in l[-2].iteritems():
keytotal = a+c
valtotal = x+e
last = l[-1]
if keytotal in last:
if last[keytotal] < valtotal:
last[keytotal] = valtotal
else:
last[keytotal] = valtotal

Could probably simplify that even more by using min(), but I don't know
what kind of data you are expecting....
last[keytotal] = min(last.get(keytotal), valtotal)
comes close to working - it would if you were doing max.
--
- Justin

Jul 18 '06 #4
On 18/07/2006 9:51 PM, Brian Elmegaard wrote:
Brian Elmegaard <br***@rkspeed-rugby.dkwrites:

At least it was clumsy to post a wrong example.
This shows what = find clumsy.

c=1
x=2

l=list()
l.append(dict())
l[0][5]=0

l.append(dict())

for a, e in l[-2].iteritems():
######### Can this be written better?
Definitely.
1. Use meaningful names. Especially don't use 'L'.lower() as it's too
easily confused with the digit '1'.
2. Put spaces around operators -- in general, RTFStyleGuide
http://www.python.org/dev/peps/pep-0008
3. When you find yourself typing the same expression 4 times, it's time
to give it a name of its own (or to throw away a usage or two).

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
Here's a start. Only you know what *really* meaningful names you should
be using.

mykey = a + c
newvalue = x + e
lastdict = dictlist[-1]
if mykey not in lastdict or lastdict[mykey] < newvalue:
lastdict[mykey] = newvalue

4. Add some comments about what you are trying to achieve. What is the
point of keeping the two dicts in a list??? Can't you give them names,
like rawdatadict and maxdict?

HTH,
John
Jul 18 '06 #5
John Machin <sj******@lexicon.netwrites:
2. Put spaces around operators -- in general, RTFStyleGuide
http://www.python.org/dev/peps/pep-0008
I din't know it. Thanks.
Only you know what *really* meaningful names you should be using.
I have better names in my running code.
mykey = a + c
newvalue = x + e
lastdict = dictlist[-1]
if mykey not in lastdict or lastdict[mykey] < newvalue:
lastdict[mykey] = newvalue
Better, thanks.
4. Add some comments about what you are trying to achieve. What is the
point of keeping the two dicts in a list??? Can't you give them
names, like rawdatadict and maxdict?
It's a dynamic programming problem, so this will contain as many dicts
as hours in the year.

--
Brian (remove the sport for mail)
http://www.et.web.mek.dtu.dk/Staff/be/be.html
Rugbyklubben Speed Scandinavian Open 7s Rugby http://www.rkspeed.dk
Jul 18 '06 #6
"Justin Azoff" <ju**********@gmail.comwrites:
last[keytotal] = min(last.get(keytotal), valtotal)
comes close to working - it would if you were doing max.
Thanks, I think this would help.

--
Brian (remove the sport for mail)
http://www.et.web.mek.dtu.dk/Staff/be/be.html
Rugbyklubben Speed Scandinavian Open 7s Rugby http://www.rkspeed.dk
Jul 18 '06 #7
"Nick Vatamaniuc" <va******@gmail.comwrites:
if l[-1].setdefault(a+c, x+e)<x+e:
l[-1][a+c]=x+e
Thanks for the answer. I will try it.

--
Brian (remove the sport for mail)
http://www.et.web.mek.dtu.dk/Staff/be/be.html
Rugbyklubben Speed Scandinavian Open 7s Rugby http://www.rkspeed.dk
Jul 18 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Rodd Snook | last post by:
I have an application which makes extensive use of the Scripting.Dictionary object. I'm not doing anything silly like putting them outside the page scope -- just creating quite a few of them and...
8
by: akameswaran | last post by:
I wrote up a quick little set of tests, I was acutally comparing ways of doing "case" behavior just to get some performance information. Now two of my test cases had almost identical results which...
18
by: Marko.Cain.23 | last post by:
Hi, I create a dictionary like this myDict = {} and I add entry like this: myDict = 1 but how can I empty the whole dictionary? Thank you.
11
by: Dan Upton | last post by:
This might be more information than necessary, but it's the best way I can think of to describe the question without being too vague. The task: I have a list of processes (well, strings to...
0
by: Gary Herron | last post by:
Dan Upton wrote: Yes. Create a list of keys, and loop through it: pids = procs_dict.keys() for pid in pids: if procs_dict.poll() != None # do the counter updates del procs_dict Then the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.