dictionary decomposition and creating a new dictionary with identical values












4















I have a dictionary myDict



{'1': 5, '2': 13, '3': 23, '4': 17}


I'm using this code, that has served me well, in order to find the key/value in myDict closest to a targetVal



answer = key, value = min(myDict.items(), key=lambda (_, v): abs(v - targetVal))


Assuming targetVal is 14, answer returns:



('2': 13)


What I need to do now, is deal with identical values in myDict. For example, if myDict was now:



{'1': 5, '2': 13, '3': 23, '4': 13}


I need both those key/value pairs with the value 13.
In instances when the code (above) finds the closest value in myDict, and that value happens to appear more than once, i'd like to create a new dictionary. In this case, answer would return:



{'2': 13, '4': 13}


Is it possible to update the way answer is being found to account for instances when the closest value appears more than once?










share|improve this question

























  • {k:v for k,v in myDict.items() if v == answer[1]}

    – Chris_Rands
    Nov 24 '18 at 22:40








  • 3





    dict is not a good data structure for this kind of task.

    – wim
    Nov 24 '18 at 22:40











  • I'll take a list of tuples, but i've been working with dictionaries lately, so i'm more comfortable manipulating them

    – yodish
    Nov 24 '18 at 22:44













  • @Chris_Rands, isn't answer just the 1 key/value though?

    – yodish
    Nov 24 '18 at 22:46











  • Did you not try my solution? answer[1] is the value, it works

    – Chris_Rands
    Nov 24 '18 at 23:00
















4















I have a dictionary myDict



{'1': 5, '2': 13, '3': 23, '4': 17}


I'm using this code, that has served me well, in order to find the key/value in myDict closest to a targetVal



answer = key, value = min(myDict.items(), key=lambda (_, v): abs(v - targetVal))


Assuming targetVal is 14, answer returns:



('2': 13)


What I need to do now, is deal with identical values in myDict. For example, if myDict was now:



{'1': 5, '2': 13, '3': 23, '4': 13}


I need both those key/value pairs with the value 13.
In instances when the code (above) finds the closest value in myDict, and that value happens to appear more than once, i'd like to create a new dictionary. In this case, answer would return:



{'2': 13, '4': 13}


Is it possible to update the way answer is being found to account for instances when the closest value appears more than once?










share|improve this question

























  • {k:v for k,v in myDict.items() if v == answer[1]}

    – Chris_Rands
    Nov 24 '18 at 22:40








  • 3





    dict is not a good data structure for this kind of task.

    – wim
    Nov 24 '18 at 22:40











  • I'll take a list of tuples, but i've been working with dictionaries lately, so i'm more comfortable manipulating them

    – yodish
    Nov 24 '18 at 22:44













  • @Chris_Rands, isn't answer just the 1 key/value though?

    – yodish
    Nov 24 '18 at 22:46











  • Did you not try my solution? answer[1] is the value, it works

    – Chris_Rands
    Nov 24 '18 at 23:00














4












4








4








I have a dictionary myDict



{'1': 5, '2': 13, '3': 23, '4': 17}


I'm using this code, that has served me well, in order to find the key/value in myDict closest to a targetVal



answer = key, value = min(myDict.items(), key=lambda (_, v): abs(v - targetVal))


Assuming targetVal is 14, answer returns:



('2': 13)


What I need to do now, is deal with identical values in myDict. For example, if myDict was now:



{'1': 5, '2': 13, '3': 23, '4': 13}


I need both those key/value pairs with the value 13.
In instances when the code (above) finds the closest value in myDict, and that value happens to appear more than once, i'd like to create a new dictionary. In this case, answer would return:



{'2': 13, '4': 13}


Is it possible to update the way answer is being found to account for instances when the closest value appears more than once?










share|improve this question
















I have a dictionary myDict



{'1': 5, '2': 13, '3': 23, '4': 17}


I'm using this code, that has served me well, in order to find the key/value in myDict closest to a targetVal



answer = key, value = min(myDict.items(), key=lambda (_, v): abs(v - targetVal))


Assuming targetVal is 14, answer returns:



('2': 13)


What I need to do now, is deal with identical values in myDict. For example, if myDict was now:



{'1': 5, '2': 13, '3': 23, '4': 13}


I need both those key/value pairs with the value 13.
In instances when the code (above) finds the closest value in myDict, and that value happens to appear more than once, i'd like to create a new dictionary. In this case, answer would return:



{'2': 13, '4': 13}


Is it possible to update the way answer is being found to account for instances when the closest value appears more than once?







python python-2.7 dictionary python-2.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 22:50









timgeb

50.6k116393




50.6k116393










asked Nov 24 '18 at 22:37









yodishyodish

329312




329312













  • {k:v for k,v in myDict.items() if v == answer[1]}

    – Chris_Rands
    Nov 24 '18 at 22:40








  • 3





    dict is not a good data structure for this kind of task.

    – wim
    Nov 24 '18 at 22:40











  • I'll take a list of tuples, but i've been working with dictionaries lately, so i'm more comfortable manipulating them

    – yodish
    Nov 24 '18 at 22:44













  • @Chris_Rands, isn't answer just the 1 key/value though?

    – yodish
    Nov 24 '18 at 22:46











  • Did you not try my solution? answer[1] is the value, it works

    – Chris_Rands
    Nov 24 '18 at 23:00



















  • {k:v for k,v in myDict.items() if v == answer[1]}

    – Chris_Rands
    Nov 24 '18 at 22:40








  • 3





    dict is not a good data structure for this kind of task.

    – wim
    Nov 24 '18 at 22:40











  • I'll take a list of tuples, but i've been working with dictionaries lately, so i'm more comfortable manipulating them

    – yodish
    Nov 24 '18 at 22:44













  • @Chris_Rands, isn't answer just the 1 key/value though?

    – yodish
    Nov 24 '18 at 22:46











  • Did you not try my solution? answer[1] is the value, it works

    – Chris_Rands
    Nov 24 '18 at 23:00

















{k:v for k,v in myDict.items() if v == answer[1]}

– Chris_Rands
Nov 24 '18 at 22:40







{k:v for k,v in myDict.items() if v == answer[1]}

– Chris_Rands
Nov 24 '18 at 22:40






3




3





dict is not a good data structure for this kind of task.

– wim
Nov 24 '18 at 22:40





dict is not a good data structure for this kind of task.

– wim
Nov 24 '18 at 22:40













I'll take a list of tuples, but i've been working with dictionaries lately, so i'm more comfortable manipulating them

– yodish
Nov 24 '18 at 22:44







I'll take a list of tuples, but i've been working with dictionaries lately, so i'm more comfortable manipulating them

– yodish
Nov 24 '18 at 22:44















@Chris_Rands, isn't answer just the 1 key/value though?

– yodish
Nov 24 '18 at 22:46





@Chris_Rands, isn't answer just the 1 key/value though?

– yodish
Nov 24 '18 at 22:46













Did you not try my solution? answer[1] is the value, it works

– Chris_Rands
Nov 24 '18 at 23:00





Did you not try my solution? answer[1] is the value, it works

– Chris_Rands
Nov 24 '18 at 23:00












2 Answers
2






active

oldest

votes


















3














Find the minimum value first, then filter your dict.



>>> d = {'1': 5, '2': 13, '3': 23, '4': 13}
>>> target = 13
>>> min_ = min(d.itervalues(), key=lambda v: abs(v - target))
>>> {k:v for k,v in d.iteritems() if v == min_}
{'2': 13, '4': 13}





share|improve this answer



















  • 1





    Thank you! I'm working again..

    – yodish
    Nov 24 '18 at 23:02



















2














As you found, min provides only one item which satisfies the minimum condition. You can construct a one-pass solution via a manual loop:



from math import inf

myDict = {'1': 5, '2': 13, '3': 23, '4': 13}
targetVal = 14

res = {}
diff = inf
for k, v in myDict.iteritems():
current_diff = abs(v - targetVal)
if current_diff <= diff:
if current_diff < diff:
diff = current_diff
res.clear()
res.update({k: v})

print(res)

# {'2': 13, '4': 13}





share|improve this answer



















  • 1





    This is good too, thank you!

    – yodish
    Nov 24 '18 at 23:03






  • 1





    float('inf') should be fine too in case you don't want to import from math.

    – timgeb
    Nov 24 '18 at 23:05






  • 1





    @timgeb, Thanks, learn something new! They are, of course, equivalent.

    – jpp
    Nov 24 '18 at 23:06













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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462962%2fdictionary-decomposition-and-creating-a-new-dictionary-with-identical-values%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














Find the minimum value first, then filter your dict.



>>> d = {'1': 5, '2': 13, '3': 23, '4': 13}
>>> target = 13
>>> min_ = min(d.itervalues(), key=lambda v: abs(v - target))
>>> {k:v for k,v in d.iteritems() if v == min_}
{'2': 13, '4': 13}





share|improve this answer



















  • 1





    Thank you! I'm working again..

    – yodish
    Nov 24 '18 at 23:02
















3














Find the minimum value first, then filter your dict.



>>> d = {'1': 5, '2': 13, '3': 23, '4': 13}
>>> target = 13
>>> min_ = min(d.itervalues(), key=lambda v: abs(v - target))
>>> {k:v for k,v in d.iteritems() if v == min_}
{'2': 13, '4': 13}





share|improve this answer



















  • 1





    Thank you! I'm working again..

    – yodish
    Nov 24 '18 at 23:02














3












3








3







Find the minimum value first, then filter your dict.



>>> d = {'1': 5, '2': 13, '3': 23, '4': 13}
>>> target = 13
>>> min_ = min(d.itervalues(), key=lambda v: abs(v - target))
>>> {k:v for k,v in d.iteritems() if v == min_}
{'2': 13, '4': 13}





share|improve this answer













Find the minimum value first, then filter your dict.



>>> d = {'1': 5, '2': 13, '3': 23, '4': 13}
>>> target = 13
>>> min_ = min(d.itervalues(), key=lambda v: abs(v - target))
>>> {k:v for k,v in d.iteritems() if v == min_}
{'2': 13, '4': 13}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 22:49









timgebtimgeb

50.6k116393




50.6k116393








  • 1





    Thank you! I'm working again..

    – yodish
    Nov 24 '18 at 23:02














  • 1





    Thank you! I'm working again..

    – yodish
    Nov 24 '18 at 23:02








1




1





Thank you! I'm working again..

– yodish
Nov 24 '18 at 23:02





Thank you! I'm working again..

– yodish
Nov 24 '18 at 23:02













2














As you found, min provides only one item which satisfies the minimum condition. You can construct a one-pass solution via a manual loop:



from math import inf

myDict = {'1': 5, '2': 13, '3': 23, '4': 13}
targetVal = 14

res = {}
diff = inf
for k, v in myDict.iteritems():
current_diff = abs(v - targetVal)
if current_diff <= diff:
if current_diff < diff:
diff = current_diff
res.clear()
res.update({k: v})

print(res)

# {'2': 13, '4': 13}





share|improve this answer



















  • 1





    This is good too, thank you!

    – yodish
    Nov 24 '18 at 23:03






  • 1





    float('inf') should be fine too in case you don't want to import from math.

    – timgeb
    Nov 24 '18 at 23:05






  • 1





    @timgeb, Thanks, learn something new! They are, of course, equivalent.

    – jpp
    Nov 24 '18 at 23:06


















2














As you found, min provides only one item which satisfies the minimum condition. You can construct a one-pass solution via a manual loop:



from math import inf

myDict = {'1': 5, '2': 13, '3': 23, '4': 13}
targetVal = 14

res = {}
diff = inf
for k, v in myDict.iteritems():
current_diff = abs(v - targetVal)
if current_diff <= diff:
if current_diff < diff:
diff = current_diff
res.clear()
res.update({k: v})

print(res)

# {'2': 13, '4': 13}





share|improve this answer



















  • 1





    This is good too, thank you!

    – yodish
    Nov 24 '18 at 23:03






  • 1





    float('inf') should be fine too in case you don't want to import from math.

    – timgeb
    Nov 24 '18 at 23:05






  • 1





    @timgeb, Thanks, learn something new! They are, of course, equivalent.

    – jpp
    Nov 24 '18 at 23:06
















2












2








2







As you found, min provides only one item which satisfies the minimum condition. You can construct a one-pass solution via a manual loop:



from math import inf

myDict = {'1': 5, '2': 13, '3': 23, '4': 13}
targetVal = 14

res = {}
diff = inf
for k, v in myDict.iteritems():
current_diff = abs(v - targetVal)
if current_diff <= diff:
if current_diff < diff:
diff = current_diff
res.clear()
res.update({k: v})

print(res)

# {'2': 13, '4': 13}





share|improve this answer













As you found, min provides only one item which satisfies the minimum condition. You can construct a one-pass solution via a manual loop:



from math import inf

myDict = {'1': 5, '2': 13, '3': 23, '4': 13}
targetVal = 14

res = {}
diff = inf
for k, v in myDict.iteritems():
current_diff = abs(v - targetVal)
if current_diff <= diff:
if current_diff < diff:
diff = current_diff
res.clear()
res.update({k: v})

print(res)

# {'2': 13, '4': 13}






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 23:00









jppjpp

96.7k2158109




96.7k2158109








  • 1





    This is good too, thank you!

    – yodish
    Nov 24 '18 at 23:03






  • 1





    float('inf') should be fine too in case you don't want to import from math.

    – timgeb
    Nov 24 '18 at 23:05






  • 1





    @timgeb, Thanks, learn something new! They are, of course, equivalent.

    – jpp
    Nov 24 '18 at 23:06
















  • 1





    This is good too, thank you!

    – yodish
    Nov 24 '18 at 23:03






  • 1





    float('inf') should be fine too in case you don't want to import from math.

    – timgeb
    Nov 24 '18 at 23:05






  • 1





    @timgeb, Thanks, learn something new! They are, of course, equivalent.

    – jpp
    Nov 24 '18 at 23:06










1




1





This is good too, thank you!

– yodish
Nov 24 '18 at 23:03





This is good too, thank you!

– yodish
Nov 24 '18 at 23:03




1




1





float('inf') should be fine too in case you don't want to import from math.

– timgeb
Nov 24 '18 at 23:05





float('inf') should be fine too in case you don't want to import from math.

– timgeb
Nov 24 '18 at 23:05




1




1





@timgeb, Thanks, learn something new! They are, of course, equivalent.

– jpp
Nov 24 '18 at 23:06







@timgeb, Thanks, learn something new! They are, of course, equivalent.

– jpp
Nov 24 '18 at 23:06




















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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53462962%2fdictionary-decomposition-and-creating-a-new-dictionary-with-identical-values%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)