Dict efficiency
Posted: Tue Apr 17, 2018 4:03 am
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)
I presume option 2 is better than 1, but where does 3 fit?
Sent from my iPhone using Tapatalk Pro
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