1

I am getting a syntax error while trying to index a python dict:

(Pdb) o_model.flows
{(<oemof.solph.network.Bus object at 0x7f3e9c6b3ea8>, <oemof.solph.network.Transformer object at 0x7f3e9c52ce08>): <oemof.solph.network.Flow object at 0x7f3e9c50d5f8>}

Here is the key of the dict.:

(Pdb) o_model.flows.keys()
dict_keys([(<oemof.solph.network.Bus object at 0x7f3e9c6b3ea8>, <oemof.solph.network.Transformer object at 0x7f3e9c52ce08>)])

So what I am assuming is the key of the dict is (<oemof.solph.network.Bus object at 0x7f3e9c6b3ea8>, <oemof.solph.network.Transformer object at 0x7f3e9c52ce08>)

Problem is that I get an syntax error, while trying to index the o_model.flows with the key, which is mentioned above.

Normally I was expecting to get the value(<oemof.solph.network.Flow object at 0x7f3e9c50d5f8>) of the dict via, but instead I get an syntax error:

(Pdb) o_model.flows[(<oemof.solph.network.Bus object at 0x7f3e9c6b3ea8>, <oemof.solph.network.Transformer object at 0x7f3e9c52ce08>)]
*** SyntaxError: invalid syntax

What I do wrong?

Some Extras:

(Pdb) type(o_model.flows)
<class 'dict'>
oakca
  • 1,408
  • 1
  • 18
  • 40
  • 3
    this is not the key of the dictionary, only a represenation !!!!!!! – Xatyrian Nov 21 '18 at 11:03
  • how would I get the key then? – oakca Nov 21 '18 at 11:04
  • 1
    I have no idea what a `oemof.solph.network.Bus object` is (presumably it's a class defined in some library you're using) - but it's definitely not the same as a dict – Robin Zigmond Nov 21 '18 at 11:04
  • Please read ["Understanding repr( ) function in Python"](https://stackoverflow.com/questions/7784148/understanding-repr-function-in-python). It does not provide you with an answer, but does give you some insight as to what those printouts mean. – Ilja Everilä Nov 21 '18 at 11:04
  • 1
    First, just check values in dictionary - use `print` function. I guess you'll find out, that that one value is not dictionary key... – kosist Nov 21 '18 at 11:04
  • @oakca `o_model.flows.keys()|0]` to get the first key – Xatyrian Nov 21 '18 at 11:06
  • @Xatyrian `*** TypeError: 'dict_keys' object does not support indexing` – oakca Nov 21 '18 at 11:06
  • Does `my_key = list(o_model.flows.keys())[0]` work? I see your key is a tuple of two objects (Bus, Transformer), so in order to index it I suppose you have to store that tuple somewhere when that dictionary is created in order to access it later – gustavovelascoh Nov 21 '18 at 11:08
  • 2
    **You can iterate over a dict without needing to know its keys, see [`values()`, `items()`](https://docs.python.org/3/library/stdtypes.html#mapping-types-dict)**. But this dict looks really painful, show us the code that generated `o_model.flows`. Its keys are not strings, they're lists of objects. Handling those keys would just be a pain. So don't construct it that way, if at all possible. – smci Nov 21 '18 at 11:08
  • to be honest if `o_model.flows.keys()` does not give me keys of the dict... why it is even working. I mean lets assume `o_model.flows` is not a python dict, then why does `.keys()` function even work??? – oakca Nov 21 '18 at 11:08
  • oakca: What is `type(o_model.flows)`. Let's start at the beginning. – smci Nov 21 '18 at 11:09
  • @smci `` and I don't want to iterate over keys. I want to get the value using the key... – oakca Nov 21 '18 at 11:11
  • yep, dictionaries are not sorted. `for key, value in o_model.flows.items(): print(key, value)` etc. If you already know the key: `variable = o_model.flows.get(key)`. Indeed you seem to have a very funky type used for your keys. Consider to use something more sane. – planetmaker Nov 21 '18 at 11:12
  • @planetmaker `(, ) ` so the key is the tuple that consist of 2 objects... I just don't understand why I can't index some dictionary, with a tuple-key consist of 2 objects – oakca Nov 21 '18 at 11:14
  • oakca: my point was iterating over a dict until you find the key/value you're looking for can be a less painful way of looking up the dict (admittedly O(N)), if they key is really painful, such as here. – smci Nov 22 '18 at 04:32

1 Answers1

2

Your key is a tuple of two objects (Bus, Transformer), so in order to index it, I suppose you have to store that tuple somewhere when that dictionary is created in order to access it later or to extract the key. You can use this:

my_key = list(o_model.flows.keys())[0]
print(o_model.flows[my_key])

Example:

test = {("qwe","zxc"): [4,5,6]}
print(test.keys()) # dict_keys([('qwe', 'zxc')])
my_key = list(testprint(.keys())[0]
print(flow[my_key]) # [4 5 6]
  • Why can't just type (<oemof.solph.network.Bus object at 0x7f3e9c6b3ea8>, <oemof.solph.network.Transformer object at 0x7f3e9c52ce08>) as key?

Because that is just the human-readable representation of that objects given that there is no string assigned for printing. Common keys, as strings, are also objects at certain location e.g. (<str object at 0x7f45f4f52c36>), but its bytes are intended to be interpreted as characters when printed.

So you don't use what is printed for indexing, you should use the object itself.

Example:

class ObjNoStr():
    def __init__(self, x):
        self.x = x

class ObjStr():
    def __init__(self, x):
        self.x = x

    def __str__(self):
        return "I have x: %d" % self.x

o1 = ObjNoStr(3)
o2 = ObjStr(3)
print(o1) # <__main__.ObjNoStr object at 0x7f36d38469b0>
print(o2) # I have x: 3
gustavovelascoh
  • 1,208
  • 1
  • 14
  • 28
  • this works, but I don't understand why I have to store it? why can't I just type it between `[brackets]` – oakca Nov 21 '18 at 11:25