Getting a value of a python dict
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'>
python dictionary indexing
|
show 10 more comments
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'>
python dictionary indexing
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 aoemof.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
1
First, just check values in dictionary - useprint
function. I guess you'll find out, that that one value is not dictionary key...
– kosist
Nov 21 '18 at 11:04
2
You can iterate over a dict without needing to know its keys, seevalues()
,items()
. But this dict looks really painful, show us the code that generatedo_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
|
show 10 more comments
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'>
python dictionary indexing
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'>
python dictionary indexing
python dictionary indexing
edited Nov 21 '18 at 11:21
oakca
asked Nov 21 '18 at 11:00
oakcaoakca
354112
354112
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 aoemof.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
1
First, just check values in dictionary - useprint
function. I guess you'll find out, that that one value is not dictionary key...
– kosist
Nov 21 '18 at 11:04
2
You can iterate over a dict without needing to know its keys, seevalues()
,items()
. But this dict looks really painful, show us the code that generatedo_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
|
show 10 more comments
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 aoemof.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
1
First, just check values in dictionary - useprint
function. I guess you'll find out, that that one value is not dictionary key...
– kosist
Nov 21 '18 at 11:04
2
You can iterate over a dict without needing to know its keys, seevalues()
,items()
. But this dict looks really painful, show us the code that generatedo_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
3
3
this is not the key of the dictionary, only a represenation !!!!!!!
– Xatyrian
Nov 21 '18 at 11:03
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
how would I get the key then?
– oakca
Nov 21 '18 at 11:04
1
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
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
1
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
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
2
2
You can iterate over a dict without needing to know its keys, see
values()
, items()
. 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
You can iterate over a dict without needing to know its keys, see
values()
, items()
. 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
|
show 10 more comments
1 Answer
1
active
oldest
votes
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
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
Hi @oakca I updated the answer.
– gustavovelascoh
Nov 21 '18 at 11:36
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410678%2fgetting-a-value-of-a-python-dict%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
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
Hi @oakca I updated the answer.
– gustavovelascoh
Nov 21 '18 at 11:36
add a comment |
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
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
Hi @oakca I updated the answer.
– gustavovelascoh
Nov 21 '18 at 11:36
add a comment |
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
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
edited Nov 21 '18 at 11:42
answered Nov 21 '18 at 11:21
gustavovelascohgustavovelascoh
5301417
5301417
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
Hi @oakca I updated the answer.
– gustavovelascoh
Nov 21 '18 at 11:36
add a comment |
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
Hi @oakca I updated the answer.
– gustavovelascoh
Nov 21 '18 at 11:36
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
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
Hi @oakca I updated the answer.
– gustavovelascoh
Nov 21 '18 at 11:36
Hi @oakca I updated the answer.
– gustavovelascoh
Nov 21 '18 at 11:36
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53410678%2fgetting-a-value-of-a-python-dict%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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
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
2
You can iterate over a dict without needing to know its keys, see
values()
,items()
. But this dict looks really painful, show us the code that generatedo_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