根据匹配的键生成值列表

时间:2019-03-20 20:51:14

标签: python-3.7 dictionary-comprehension

我有以下字典:

{'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}}

如何完成创建包含指定键的所有值的列表?是所有高值的列表,还是所有中值的另一个列表?

我目前完成此操作的方式似乎并不是最好的方法。我有所有严重性级别的列表,我对其进行了迭代和比较,如下所示:

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])

1 个答案:

答案 0 :(得分:1)

注意:您正在遍历total_status_dict键,并将结果附加到列表中。请记住,即使从3.7开始(在https://docs.python.org/3/whatsnew/3.7.html中使用Python正式订购字典,您也不总是控制用户的Python版本。我宁愿构建一个字典key -> item -> value,其中keyClosedImpact Statement Pending,...,而itemtrace_labels之一字典key -> [values],其中values应该按照trace_labels中的顺序排序。

您的代码效率不高,因为您在trace_labels上进行了两次迭代:

  • for item in trace_labels:
  • for ke in total_status_dict [key]:如果项目为ke:`

如何仅迭代一次?您可以一次构建多个列表,而不必一次一个地构建y_values列表(每次遍历total_status_dict一次)。

>>> 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, {})如果y_values_by_label[label] = {}没有密钥y_values_by_label,则会创建一个空的字典label

如果要通过dict理解来解决这个问题,则必须使用效率低下的方法:

>>> {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}}