Page 1 of 1

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

Posted: Fri Oct 26, 2018 9:30 pm
by Mastiff
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?

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

Posted: Sat Oct 27, 2018 11:53 am
by topix
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.

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

Posted: Sun Oct 28, 2018 9:02 am
by Mastiff
Thank you very much, that works! And my mistake, tables is what it's called in LUA. :)

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

Posted: Mon Oct 29, 2018 11:25 pm
by kgschlosser
@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.

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

Posted: Sun Nov 04, 2018 11:35 am
by topix
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.

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

Posted: Sun Nov 04, 2018 4:42 pm
by kgschlosser
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!