Dict efficiency

Posted on
Tue Apr 17, 2018 4:03 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Dict efficiency

I’ve got a dict of about 30 keys, which I push straight into matching dev states.

However about 8 of the keys don’t make sense to the end user so I want to intercept and change them on the fly.

Which is best / what’s the best way:

(I’m typing on phone from memory so syntax is intentionally sloppy)

Code: Select all
#Check k several times each loop
for k,v in myDict:
  if k == “abc”, k=“newabc”
  if k == “def”, k = “newdef”
...etc
dev.states[k]=v
   


Code: Select all
#Check k once per loop, then only check again if it’s in list of swaps
for k,v in myDict:
  if k in kSwapsList:
     if k == “abc”, k=“newabc”
     if k == “def”, k = “newdef”
...etc
dev.states[k]=v
   


Code: Select all
#Brute force without checking
for k,v in myDict:
  k = replace(“abc”,”newabc”)
  k = replace(“abc”,”newabc”)
...etc
dev.states[k]=v
   


I presume option 2 is better than 1, but where does 3 fit?


Sent from my iPhone using Tapatalk Pro

Posted on
Tue Apr 17, 2018 5:43 am
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Dict efficiency

If I know that I'm only going to do the change once per cycle, I prefer something like:
Code: Select all
for k in myDict.keys():
    if myDict[k] == "abc": myDict[k] = "newabc"
    if myDict[k] == "def": myDict[k] = "newdef"

But if efficiency is your ultimate goal, you can always test various constructions using timeit:
https://docs.python.org/2/library/timeit.html

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Apr 17, 2018 6:02 am
howartp offline
Posts: 4559
Joined: Jan 09, 2014
Location: West Yorkshire, UK

Re: Dict efficiency

It’s the key names, not values, I’m testing/updating.

Tesla API gives you loads of state/value pairs, but door states are “fl”, “fr”, “rl” and “rr” off top of my head.

I want to intercept and name them “door_fl” etc, every time the car status is refreshed.

Peter


Sent from my iPhone using Tapatalk Pro

Posted on
Tue Apr 17, 2018 6:09 am
DaveL17 offline
User avatar
Posts: 6742
Joined: Aug 20, 2013
Location: Chicago, IL, USA

Re: Dict efficiency

Sorry, missed that bit. I think this will work for unordered dicts:
Code: Select all
for k in myDict.keys():
    if k == "abc": myDict["newabc"] = myDict.pop(k)

I came here to drink milk and kick ass....and I've just finished my milk.

[My Plugins] - [My Forums]

Posted on
Tue Apr 17, 2018 6:14 am
FlyingDiver offline
User avatar
Posts: 7189
Joined: Jun 07, 2014
Location: Southwest Florida, USA

Re: Dict efficiency

I probably wouldn't loop at all.

Code: Select all
myDict["new_abc"] = myDict["abc"]
del myDict["abc"]

myDict["new_def"] = myDict["def"]
del myDict["def"]

for k,v in myDict:
    dev.states[k]=v


Or maybe:

Code: Select all
for k,v in myDict:
    if k == “abc”: dev.states["new_abc"]=v
    elif k == “def":  dev.states["new_def"]=v
    else: dev.states[k]=v

joe (aka FlyingDiver)
my plugins: http://forums.indigodomo.com/viewforum.php?f=177

Posted on
Tue Apr 17, 2018 10:23 am
kmarkley offline
Posts: 185
Joined: Nov 15, 2016

Re: Dict efficiency

I suspect it is not very efficient, but for simplicity I like to use a translation dictionary:
Code: Select all
kStateNames = {
   'abc': 'newabc',
   'def': 'newdef',
   }

for k,v in myDict.items():
   dev.states[kStateNames.get(k,k)] = v

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 5 guests