How to get values from a defaultdict using a new combination of keys?











up vote
-1
down vote

favorite












I have the following defaultdict:



dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}}


From this dict, I need to append the values to a list using the following combination of keys:



order = [("b","b"), ("b","a"), ("b","c"), ("a","b", ("a","a"), ("a","c"), ("c","b"), ("c","a"), ("c","c")]


If the keys combination are the same (for example ("a", "a")), the value should be zero. So the final result should look something like the following:



result = [0, 0.1, 0.3, 0.1, 0, 0.2, 0.3, 0.2, 0]


I have tried the following



 dist = 
for index1, member1 in enumerate(order):
curr = dictA.get(member1, {})
for index2, member2 in enumerate(order):
val = curr.get(member2)
if member1 == member2:
val = 0
if member2 not in curr:
val = None
dist.append(val)


But obviously this is not working as intended. Can someone help me with this? Thank you










share|improve this question


















  • 1




    Is dictA arbitrarily deep or does it just have 1 level?
    – slider
    Nov 19 at 23:01










  • It only has one level
    – Giratina86
    Nov 19 at 23:51















up vote
-1
down vote

favorite












I have the following defaultdict:



dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}}


From this dict, I need to append the values to a list using the following combination of keys:



order = [("b","b"), ("b","a"), ("b","c"), ("a","b", ("a","a"), ("a","c"), ("c","b"), ("c","a"), ("c","c")]


If the keys combination are the same (for example ("a", "a")), the value should be zero. So the final result should look something like the following:



result = [0, 0.1, 0.3, 0.1, 0, 0.2, 0.3, 0.2, 0]


I have tried the following



 dist = 
for index1, member1 in enumerate(order):
curr = dictA.get(member1, {})
for index2, member2 in enumerate(order):
val = curr.get(member2)
if member1 == member2:
val = 0
if member2 not in curr:
val = None
dist.append(val)


But obviously this is not working as intended. Can someone help me with this? Thank you










share|improve this question


















  • 1




    Is dictA arbitrarily deep or does it just have 1 level?
    – slider
    Nov 19 at 23:01










  • It only has one level
    – Giratina86
    Nov 19 at 23:51













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I have the following defaultdict:



dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}}


From this dict, I need to append the values to a list using the following combination of keys:



order = [("b","b"), ("b","a"), ("b","c"), ("a","b", ("a","a"), ("a","c"), ("c","b"), ("c","a"), ("c","c")]


If the keys combination are the same (for example ("a", "a")), the value should be zero. So the final result should look something like the following:



result = [0, 0.1, 0.3, 0.1, 0, 0.2, 0.3, 0.2, 0]


I have tried the following



 dist = 
for index1, member1 in enumerate(order):
curr = dictA.get(member1, {})
for index2, member2 in enumerate(order):
val = curr.get(member2)
if member1 == member2:
val = 0
if member2 not in curr:
val = None
dist.append(val)


But obviously this is not working as intended. Can someone help me with this? Thank you










share|improve this question













I have the following defaultdict:



dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}}


From this dict, I need to append the values to a list using the following combination of keys:



order = [("b","b"), ("b","a"), ("b","c"), ("a","b", ("a","a"), ("a","c"), ("c","b"), ("c","a"), ("c","c")]


If the keys combination are the same (for example ("a", "a")), the value should be zero. So the final result should look something like the following:



result = [0, 0.1, 0.3, 0.1, 0, 0.2, 0.3, 0.2, 0]


I have tried the following



 dist = 
for index1, member1 in enumerate(order):
curr = dictA.get(member1, {})
for index2, member2 in enumerate(order):
val = curr.get(member2)
if member1 == member2:
val = 0
if member2 not in curr:
val = None
dist.append(val)


But obviously this is not working as intended. Can someone help me with this? Thank you







python defaultdict






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 at 22:58









Giratina86

31




31








  • 1




    Is dictA arbitrarily deep or does it just have 1 level?
    – slider
    Nov 19 at 23:01










  • It only has one level
    – Giratina86
    Nov 19 at 23:51














  • 1




    Is dictA arbitrarily deep or does it just have 1 level?
    – slider
    Nov 19 at 23:01










  • It only has one level
    – Giratina86
    Nov 19 at 23:51








1




1




Is dictA arbitrarily deep or does it just have 1 level?
– slider
Nov 19 at 23:01




Is dictA arbitrarily deep or does it just have 1 level?
– slider
Nov 19 at 23:01












It only has one level
– Giratina86
Nov 19 at 23:51




It only has one level
– Giratina86
Nov 19 at 23:51












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










You could do something like this:



def get(d, first, second):
return d.get(second, {}).get(first, 0.0)

dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}}
order = [("b", "b"), ("b", "a"), ("b", "c"), ("a", "b"), ("a", "a"), ("a", "c"), ("c", "b"), ("c", "a"), ("c", "c")]

result = [get(dictA, first, second) or get(dictA, second, first) for first, second in order]

print(result)


Output



[0.0, 0.1, 0.3, 0.1, 0.0, 0.2, 0.3, 0.2, 0.0]


Or the less pythonic alternative:



def get(d, first, second):
return d.get(second, {}).get(first, 0.0)


dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}}
order = [("b", "b"), ("b", "a"), ("b", "c"), ("a", "b"), ("a", "a"), ("a", "c"), ("c", "b"), ("c", "a"), ("c", "c")]

result =
for first, second in order:
value = get(dictA, first, second)
if value:
result.append(value)
else:
value = get(dictA, second, first)
result.append(value)


print(result)


Output



[0.0, 0.1, 0.3, 0.1, 0.0, 0.2, 0.3, 0.2, 0.0]





share|improve this answer





















  • This worked like a charm! I probably tried the less pythonic approach. But I see my mistake now.
    – Giratina86
    Nov 19 at 23:51











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',
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383845%2fhow-to-get-values-from-a-defaultdict-using-a-new-combination-of-keys%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








up vote
0
down vote



accepted










You could do something like this:



def get(d, first, second):
return d.get(second, {}).get(first, 0.0)

dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}}
order = [("b", "b"), ("b", "a"), ("b", "c"), ("a", "b"), ("a", "a"), ("a", "c"), ("c", "b"), ("c", "a"), ("c", "c")]

result = [get(dictA, first, second) or get(dictA, second, first) for first, second in order]

print(result)


Output



[0.0, 0.1, 0.3, 0.1, 0.0, 0.2, 0.3, 0.2, 0.0]


Or the less pythonic alternative:



def get(d, first, second):
return d.get(second, {}).get(first, 0.0)


dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}}
order = [("b", "b"), ("b", "a"), ("b", "c"), ("a", "b"), ("a", "a"), ("a", "c"), ("c", "b"), ("c", "a"), ("c", "c")]

result =
for first, second in order:
value = get(dictA, first, second)
if value:
result.append(value)
else:
value = get(dictA, second, first)
result.append(value)


print(result)


Output



[0.0, 0.1, 0.3, 0.1, 0.0, 0.2, 0.3, 0.2, 0.0]





share|improve this answer





















  • This worked like a charm! I probably tried the less pythonic approach. But I see my mistake now.
    – Giratina86
    Nov 19 at 23:51















up vote
0
down vote



accepted










You could do something like this:



def get(d, first, second):
return d.get(second, {}).get(first, 0.0)

dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}}
order = [("b", "b"), ("b", "a"), ("b", "c"), ("a", "b"), ("a", "a"), ("a", "c"), ("c", "b"), ("c", "a"), ("c", "c")]

result = [get(dictA, first, second) or get(dictA, second, first) for first, second in order]

print(result)


Output



[0.0, 0.1, 0.3, 0.1, 0.0, 0.2, 0.3, 0.2, 0.0]


Or the less pythonic alternative:



def get(d, first, second):
return d.get(second, {}).get(first, 0.0)


dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}}
order = [("b", "b"), ("b", "a"), ("b", "c"), ("a", "b"), ("a", "a"), ("a", "c"), ("c", "b"), ("c", "a"), ("c", "c")]

result =
for first, second in order:
value = get(dictA, first, second)
if value:
result.append(value)
else:
value = get(dictA, second, first)
result.append(value)


print(result)


Output



[0.0, 0.1, 0.3, 0.1, 0.0, 0.2, 0.3, 0.2, 0.0]





share|improve this answer





















  • This worked like a charm! I probably tried the less pythonic approach. But I see my mistake now.
    – Giratina86
    Nov 19 at 23:51













up vote
0
down vote



accepted







up vote
0
down vote



accepted






You could do something like this:



def get(d, first, second):
return d.get(second, {}).get(first, 0.0)

dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}}
order = [("b", "b"), ("b", "a"), ("b", "c"), ("a", "b"), ("a", "a"), ("a", "c"), ("c", "b"), ("c", "a"), ("c", "c")]

result = [get(dictA, first, second) or get(dictA, second, first) for first, second in order]

print(result)


Output



[0.0, 0.1, 0.3, 0.1, 0.0, 0.2, 0.3, 0.2, 0.0]


Or the less pythonic alternative:



def get(d, first, second):
return d.get(second, {}).get(first, 0.0)


dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}}
order = [("b", "b"), ("b", "a"), ("b", "c"), ("a", "b"), ("a", "a"), ("a", "c"), ("c", "b"), ("c", "a"), ("c", "c")]

result =
for first, second in order:
value = get(dictA, first, second)
if value:
result.append(value)
else:
value = get(dictA, second, first)
result.append(value)


print(result)


Output



[0.0, 0.1, 0.3, 0.1, 0.0, 0.2, 0.3, 0.2, 0.0]





share|improve this answer












You could do something like this:



def get(d, first, second):
return d.get(second, {}).get(first, 0.0)

dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}}
order = [("b", "b"), ("b", "a"), ("b", "c"), ("a", "b"), ("a", "a"), ("a", "c"), ("c", "b"), ("c", "a"), ("c", "c")]

result = [get(dictA, first, second) or get(dictA, second, first) for first, second in order]

print(result)


Output



[0.0, 0.1, 0.3, 0.1, 0.0, 0.2, 0.3, 0.2, 0.0]


Or the less pythonic alternative:



def get(d, first, second):
return d.get(second, {}).get(first, 0.0)


dictA = {"a": {"b": 0.1, "c": 0.2}, "b": {"c": 0.3}}
order = [("b", "b"), ("b", "a"), ("b", "c"), ("a", "b"), ("a", "a"), ("a", "c"), ("c", "b"), ("c", "a"), ("c", "c")]

result =
for first, second in order:
value = get(dictA, first, second)
if value:
result.append(value)
else:
value = get(dictA, second, first)
result.append(value)


print(result)


Output



[0.0, 0.1, 0.3, 0.1, 0.0, 0.2, 0.3, 0.2, 0.0]






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 at 23:20









Daniel Mesejo

9,5331923




9,5331923












  • This worked like a charm! I probably tried the less pythonic approach. But I see my mistake now.
    – Giratina86
    Nov 19 at 23:51


















  • This worked like a charm! I probably tried the less pythonic approach. But I see my mistake now.
    – Giratina86
    Nov 19 at 23:51
















This worked like a charm! I probably tried the less pythonic approach. But I see my mistake now.
– Giratina86
Nov 19 at 23:51




This worked like a charm! I probably tried the less pythonic approach. But I see my mistake now.
– Giratina86
Nov 19 at 23:51


















draft saved

draft discarded




















































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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383845%2fhow-to-get-values-from-a-defaultdict-using-a-new-combination-of-keys%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen