I need an array of data that has a numeric index, but also a human readable index. I need the latter because the numeric indices may change in the future, and I need the numeric indices as a part of a fixed length socket message.
My imagination suggests something like this:
ACTIONS = {
(0, "ALIVE") : (1, 4, False),
(2, "DEAD") : (2, 1, True)
}
>ACTIONS[0]
(1, 4, False)
>ACTIONS["DEAD"]
(2, 1, True)
The simplest way to achieve this is to have two dictionaries: One mapping the indices to your values, and one mapping the string keys to the same objects:
>> actions = {"alive": (1, 4, False), "dead": (2, 1, True)}
>> indexed_actions = {0: actions["alive"], 2: actions["dead"]}
>> actions["alive"]
(1, 4, False)
>> indexed_actions[0]
(1, 4, False)
Use Python 2.7's collections.OrderedDict
In [23]: d = collections.OrderedDict([
....: ("ALIVE", (1, 4, False)),
....: ("DEAD", (2, 1, True)),
....: ])
In [25]: d["ALIVE"]
Out[25]: (1, 4, False)
In [26]: d.values()[0]
Out[26]: (1, 4, False)
In [27]: d.values()[1]
Out[27]: (2, 1, True)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With