Generate list of values, based on matching keysRefactoring with python dictionary comprehensionCan python dictionary comprehension be used to create a dictionary of substrings and their locations?Dictionary Containing list data, filter based on value in listpython dictionary of dictionaries comprehension with nested loopsDelete a dictionary based on the value of a key in another list of dictionariesPython Set Comprehension Nested in Dict Comprehensiondictionary comprehension with list iteration based on value dimensionsCreating Python defaultdict using nested list of tuplesNeed help matching on a certain time a string is matched in a listPython: Print certain items from an API call that is a list and not a Dict
Succinct and gender-neutral Russian word for "writer"
Why do Thanos' punches not kill Captain America or at least cause vital wounds?
When do you stop "pushing" a book?
Why is PerfectForwardSecrecy considered OK, when it has same defects as salt-less password hashing?
Any suggestions on how to make this table prettier and suggestions on whether it's better to make it fit in 1 or 2 columns
Has magnetic core memory been used beyond the Moon?
Company threw a surprise party for the CEO, 3 weeks later management says we have to pay for it, do I have to?
What does formal training in a field mean?
Whats the purpose of lockedLoadData / uncached page build takes around a minute, spent in usleep
Are there variations of the regular runtimes of the Big-O-Notation?
Remove color cast in darktable?
Was Mohammed the most popular first name for boys born in Berlin in 2018?
Is it a good idea to copy a trader when investing?
Is it a Munchausen Number?
Why does increasing the sampling rate make implementing an anti-aliasing filter easier?
Getting a error after using setState with a promise
What's the difference between const array and static const array in C/C++
How are one-time password generators like Google Authenticator different from having two passwords?
Names of the Six Tastes
What is the name of meteoroids which hit Moon, Mars, or pretty much anything that isn’t the Earth?
Why did they go to Dragonstone?
Is it bad writing or bad story telling if first person narrative contains more information than the narrator knows?
What do "KAL." and "A.S." stand for in this inscription?
Electric kick drum pedal starts oscillating in such a way that it does not register hits
Generate list of values, based on matching keys
Refactoring with python dictionary comprehensionCan python dictionary comprehension be used to create a dictionary of substrings and their locations?Dictionary Containing list data, filter based on value in listpython dictionary of dictionaries comprehension with nested loopsDelete a dictionary based on the value of a key in another list of dictionariesPython Set Comprehension Nested in Dict Comprehensiondictionary comprehension with list iteration based on value dimensionsCreating Python defaultdict using nested list of tuplesNeed help matching on a certain time a string is matched in a listPython: Print certain items from an API call that is a list and not a Dict
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have the below dictionary:
'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
How might I accomplish creating a list comprised of all values for a specified key? A list for all high values, or another list for all medium values?
The way I am currently accomplishing this doesn't seem like the best way. I've got a list of all severity levels, which I iterate over and compare such as shown below:
trace_list = ['High', 'Medium', 'Critical', 'Low']
total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
for item in trace_labels:
y_values = []
for key, val in total_status_dict.items():
for ke in total_status_dict[key]:
if item is ke:
y_values.append(total_status_dict[key][ke])
python-3.7 dictionary-comprehension
add a comment |
I have the below dictionary:
'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
How might I accomplish creating a list comprised of all values for a specified key? A list for all high values, or another list for all medium values?
The way I am currently accomplishing this doesn't seem like the best way. I've got a list of all severity levels, which I iterate over and compare such as shown below:
trace_list = ['High', 'Medium', 'Critical', 'Low']
total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
for item in trace_labels:
y_values = []
for key, val in total_status_dict.items():
for ke in total_status_dict[key]:
if item is ke:
y_values.append(total_status_dict[key][ke])
python-3.7 dictionary-comprehension
add a comment |
I have the below dictionary:
'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
How might I accomplish creating a list comprised of all values for a specified key? A list for all high values, or another list for all medium values?
The way I am currently accomplishing this doesn't seem like the best way. I've got a list of all severity levels, which I iterate over and compare such as shown below:
trace_list = ['High', 'Medium', 'Critical', 'Low']
total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
for item in trace_labels:
y_values = []
for key, val in total_status_dict.items():
for ke in total_status_dict[key]:
if item is ke:
y_values.append(total_status_dict[key][ke])
python-3.7 dictionary-comprehension
I have the below dictionary:
'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
How might I accomplish creating a list comprised of all values for a specified key? A list for all high values, or another list for all medium values?
The way I am currently accomplishing this doesn't seem like the best way. I've got a list of all severity levels, which I iterate over and compare such as shown below:
trace_list = ['High', 'Medium', 'Critical', 'Low']
total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
for item in trace_labels:
y_values = []
for key, val in total_status_dict.items():
for ke in total_status_dict[key]:
if item is ke:
y_values.append(total_status_dict[key][ke])
python-3.7 dictionary-comprehension
python-3.7 dictionary-comprehension
edited Mar 20 at 21:52
NewToThis
asked Mar 20 at 20:51
NewToThisNewToThis
731110
731110
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Note: you are iterating over total_status_dict keys and appending results to a list. Remember that even if dictionaries are officially ordered in Python since 3.7 (see https://docs.python.org/3/whatsnew/3.7.html) you do not always control the Python version of the user. I would rather build a dict key -> item -> value, where key is Closed, Impact Statement Pending, ... and item is one of the trace_labels than a dict key -> [values] where values is supposed to be ordered as in trace_labels.
Your code is not efficient because you iterate over trace_labels twice:
for item in trace_labels:for ke intotal_status_dict[key]: if item is ke:`
How to iterate only once? Instead of building y_values lists one by one (with a whole iteration over total_status_dict each time), you can build several lists at once:
>>> trace_labels = ['High', 'Medium', 'Critical', 'Low']
>>> total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
>>> y_values_by_label =
>>> for key, value_by_label in total_status_dict.items():
... for label, value in value_by_label.items(): # total_status_dict[key] is value_by_label
... y_values_by_label.setdefault(label, )[key] = value
...
>>> y_values_by_label
'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1
setdefault(label, ) creates a empty dict y_values_by_label[label] = if y_values_by_label does not have the key label.
If you want to turn this in a dict comprehension, you have to use your inefficient method:
>>> label:k:v for k, value_by_label in total_status_dict.items() for l, v in value_by_label.items() if l==label for label in trace_labels
'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18
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%2f55269978%2fgenerate-list-of-values-based-on-matching-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
Note: you are iterating over total_status_dict keys and appending results to a list. Remember that even if dictionaries are officially ordered in Python since 3.7 (see https://docs.python.org/3/whatsnew/3.7.html) you do not always control the Python version of the user. I would rather build a dict key -> item -> value, where key is Closed, Impact Statement Pending, ... and item is one of the trace_labels than a dict key -> [values] where values is supposed to be ordered as in trace_labels.
Your code is not efficient because you iterate over trace_labels twice:
for item in trace_labels:for ke intotal_status_dict[key]: if item is ke:`
How to iterate only once? Instead of building y_values lists one by one (with a whole iteration over total_status_dict each time), you can build several lists at once:
>>> trace_labels = ['High', 'Medium', 'Critical', 'Low']
>>> total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
>>> y_values_by_label =
>>> for key, value_by_label in total_status_dict.items():
... for label, value in value_by_label.items(): # total_status_dict[key] is value_by_label
... y_values_by_label.setdefault(label, )[key] = value
...
>>> y_values_by_label
'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1
setdefault(label, ) creates a empty dict y_values_by_label[label] = if y_values_by_label does not have the key label.
If you want to turn this in a dict comprehension, you have to use your inefficient method:
>>> label:k:v for k, value_by_label in total_status_dict.items() for l, v in value_by_label.items() if l==label for label in trace_labels
'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18
add a comment |
Note: you are iterating over total_status_dict keys and appending results to a list. Remember that even if dictionaries are officially ordered in Python since 3.7 (see https://docs.python.org/3/whatsnew/3.7.html) you do not always control the Python version of the user. I would rather build a dict key -> item -> value, where key is Closed, Impact Statement Pending, ... and item is one of the trace_labels than a dict key -> [values] where values is supposed to be ordered as in trace_labels.
Your code is not efficient because you iterate over trace_labels twice:
for item in trace_labels:for ke intotal_status_dict[key]: if item is ke:`
How to iterate only once? Instead of building y_values lists one by one (with a whole iteration over total_status_dict each time), you can build several lists at once:
>>> trace_labels = ['High', 'Medium', 'Critical', 'Low']
>>> total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
>>> y_values_by_label =
>>> for key, value_by_label in total_status_dict.items():
... for label, value in value_by_label.items(): # total_status_dict[key] is value_by_label
... y_values_by_label.setdefault(label, )[key] = value
...
>>> y_values_by_label
'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1
setdefault(label, ) creates a empty dict y_values_by_label[label] = if y_values_by_label does not have the key label.
If you want to turn this in a dict comprehension, you have to use your inefficient method:
>>> label:k:v for k, value_by_label in total_status_dict.items() for l, v in value_by_label.items() if l==label for label in trace_labels
'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18
add a comment |
Note: you are iterating over total_status_dict keys and appending results to a list. Remember that even if dictionaries are officially ordered in Python since 3.7 (see https://docs.python.org/3/whatsnew/3.7.html) you do not always control the Python version of the user. I would rather build a dict key -> item -> value, where key is Closed, Impact Statement Pending, ... and item is one of the trace_labels than a dict key -> [values] where values is supposed to be ordered as in trace_labels.
Your code is not efficient because you iterate over trace_labels twice:
for item in trace_labels:for ke intotal_status_dict[key]: if item is ke:`
How to iterate only once? Instead of building y_values lists one by one (with a whole iteration over total_status_dict each time), you can build several lists at once:
>>> trace_labels = ['High', 'Medium', 'Critical', 'Low']
>>> total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
>>> y_values_by_label =
>>> for key, value_by_label in total_status_dict.items():
... for label, value in value_by_label.items(): # total_status_dict[key] is value_by_label
... y_values_by_label.setdefault(label, )[key] = value
...
>>> y_values_by_label
'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1
setdefault(label, ) creates a empty dict y_values_by_label[label] = if y_values_by_label does not have the key label.
If you want to turn this in a dict comprehension, you have to use your inefficient method:
>>> label:k:v for k, value_by_label in total_status_dict.items() for l, v in value_by_label.items() if l==label for label in trace_labels
'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18
Note: you are iterating over total_status_dict keys and appending results to a list. Remember that even if dictionaries are officially ordered in Python since 3.7 (see https://docs.python.org/3/whatsnew/3.7.html) you do not always control the Python version of the user. I would rather build a dict key -> item -> value, where key is Closed, Impact Statement Pending, ... and item is one of the trace_labels than a dict key -> [values] where values is supposed to be ordered as in trace_labels.
Your code is not efficient because you iterate over trace_labels twice:
for item in trace_labels:for ke intotal_status_dict[key]: if item is ke:`
How to iterate only once? Instead of building y_values lists one by one (with a whole iteration over total_status_dict each time), you can build several lists at once:
>>> trace_labels = ['High', 'Medium', 'Critical', 'Low']
>>> total_status_dict = 'Closed': 'High': 33, 'Medium': 474, 'Low': 47, 'Critical': 6, 'Impact Statement Pending': 'Low': 3, 'Medium': 1, 'Critical': 0, 'High': 0, 'New': 'Low': 1, 'High': 2, 'Critical': 2, 'Medium': 2, 'Remediation Plan Pending': 'Medium': 10, 'Low': 1, 'Critical': 1, 'High': 0, 'Remedy in Progress': 'Medium': 36, 'Low': 18, 'High': 4, 'Critical': 1
>>> y_values_by_label =
>>> for key, value_by_label in total_status_dict.items():
... for label, value in value_by_label.items(): # total_status_dict[key] is value_by_label
... y_values_by_label.setdefault(label, )[key] = value
...
>>> y_values_by_label
'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1
setdefault(label, ) creates a empty dict y_values_by_label[label] = if y_values_by_label does not have the key label.
If you want to turn this in a dict comprehension, you have to use your inefficient method:
>>> label:k:v for k, value_by_label in total_status_dict.items() for l, v in value_by_label.items() if l==label for label in trace_labels
'High': 'Closed': 33, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 0, 'Remedy in Progress': 4, 'Medium': 'Closed': 474, 'Impact Statement Pending': 1, 'New': 2, 'Remediation Plan Pending': 10, 'Remedy in Progress': 36, 'Critical': 'Closed': 6, 'Impact Statement Pending': 0, 'New': 2, 'Remediation Plan Pending': 1, 'Remedy in Progress': 1, 'Low': 'Closed': 47, 'Impact Statement Pending': 3, 'New': 1, 'Remediation Plan Pending': 1, 'Remedy in Progress': 18
answered Mar 23 at 9:38
jferardjferard
2,7001315
2,7001315
add a comment |
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%2f55269978%2fgenerate-list-of-values-based-on-matching-keys%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