Can I go from value in a table to the index?

Do you have questions about writing plugins or scripts in Python? Meet the coders here.
Post Reply
Mastiff
Experienced User
Posts: 872
Joined: Thu May 03, 2012 10:43 am

Can I go from value in a table to the index?

Post by Mastiff » Fri Oct 26, 2018 9:30 pm

I have a table that looks like this (it's actually fore times longer):

eg.globals.Romkonvertering = {
43776:"1",
14593:"2",
2816:"3",
27648:"4",
50432:"5",
}

The first numbers (I believe that's called an index) are temperature sensor codes, and they change whenever I replace the batteries. So I only have to change the sensor code in one place, and it's used in several scripts for different purposes. But for one script I would like to be able to do the reverse, so instead of going:

Code: Select all

print(eg.globals.Romkonvertering[50432])
to get the printout 5 (a string)

I would like to go:

Code: Select all

print(eg.globals.Romkonvertering['5'])
and get the result 50432. I know that that's not the correct code, of course. But can it be done in a quick and dirty code that I can reuse for several lines?

User avatar
topix
Experienced User
Posts: 417
Joined: Sat May 05, 2007 3:43 pm
Location: Germany
Contact:

Re: Can I go from value in a table to the index?

Post by topix » Sat Oct 27, 2018 11:53 am

In your example eg.globals.Romkonvertering is a dictionary and what you called index is a key. With dictinary[key] you get the value for the key. With the following code you can get the key for the value

Code: Select all

key = eg.globals.Romkonvertering.keys()[eg.globals.Romkonvertering.values().index(value)]
This will work as long as the value exists only once in the dictionary, else it will give the key for the first find of value.

Mastiff
Experienced User
Posts: 872
Joined: Thu May 03, 2012 10:43 am

Re: Can I go from value in a table to the index?

Post by Mastiff » Sun Oct 28, 2018 9:02 am

Thank you very much, that works! And my mistake, tables is what it's called in LUA. :)

User avatar
kgschlosser
Site Admin
Posts: 4250
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Can I go from value in a table to the index?

Post by kgschlosser » Mon Oct 29, 2018 11:25 pm

@topix

if you can answer a question I have on this.

I thought dictionaries are not indexed so to speak and therefore the order in which the elements get returned might not be in the same order as the first time you made a call.

I see that you are relying on the order of keys being returned is the exact same order as when values get returned. I thought the only way to make sure this happens is you have to use an OrderedDict??
I could be wrong.

Please explain... :D

If that is the case then you can also do this

Code: Select all

eg.globals.Romkonvertering = {
43776:"1",
14593:"2",
2816:"3",
27648:"4",
50432:"5",
}

eg.globals.ReverseRomkonvertering = dict(list((value, key) for key, value in eg.globals.Romkonvertering.items()))

print eg.globals.ReverseRomkonvertering[value]

If you have duplicate values it is going to be a crap shoot which key gets returned.
If you like the work I have been doing then feel free to Image

User avatar
topix
Experienced User
Posts: 417
Joined: Sat May 05, 2007 3:43 pm
Location: Germany
Contact:

Re: Can I go from value in a table to the index?

Post by topix » Sun Nov 04, 2018 11:35 am

I used the code found here: https://stackoverflow.com/a/13149770
Order is guaranteed not to change through iterations as long as the dictionary is not modified.

You are right that the order in a dict is not guaranteed (in python 2).
From the example posted i assumed that the values will have no duplicates.

User avatar
kgschlosser
Site Admin
Posts: 4250
Joined: Fri Jun 05, 2015 5:43 am
Location: Rocky Mountains, Colorado USA

Re: Can I go from value in a table to the index?

Post by kgschlosser » Sun Nov 04, 2018 4:42 pm

ok so long as the dict never gets changed it will remain a constant. Now I know that if i set a dict literal and never modify it the keys and values when called will always return in the same order.

I learned something new!
If you like the work I have been doing then feel free to Image

Post Reply