【问题标题】:Python: Looping over One Dictionary and Creating Key/Value Pairs in a New Dictionary if Conditions Are MetPython:如果满足条件,则遍历一个字典并在新字典中创建键/值对
【发布时间】:2011-03-10 21:10:54
【问题描述】:

我想将一个字典的值与第二个字典的值进行比较。如果值符合某些条件,我想创建第三个字典,其中的键和值对会因匹配项而异。

这是一个人为的例子来说明我的问题。

编辑:对所有返回感到抱歉,但堆栈溢出无法识别单个返回,并且在一行中运行 3-4 行,使代码难以辨认。此外,它不会将我的代码作为代码灰显。不知道为什么。

employee = {'skills': 'superintendent', 'teaches': 'social studies', 
            'grades': 'K-12'}
school_districts = {0: {'needs':  'superintendent', 'grades': 'K-12'}, 
                    1:{'needs': 'social_studies', 'grades': 'K-12'}}
jobs_in_school_district = {}
for key in school_districts:
    if (employee['skills'] == school_districts[key]['needs']):
        jobs_in_school_district[key] = {}
        jobs_in_school_district[key]['best_paying_job'] = 'superintendent'

    if (employee['teaches'] == school_districts[key]['needs']):
        jobs_in_school_district[key] = {}
        jobs_in_school_district[key]['other_job'] = 'social_studies_teacher'

print(jobs_in_school_district)

这是我希望看到的 'jobs_in_school_district' 的值:

{0: {'best_paying_job': 'superintendent'}, 
 1: {'other_job': 'social_studies_teacher'}}

这就是我得到的:

{1: {'other_job': 'social_studies_teacher'}}

我明白这里出了什么问题。 Python 在第一个 if 块(第 6-8 行)之后将jobs_in_school_district 设置为等于{0: {'best_paying_job': 'superintendent'}。然后它执行第二个 if 块(第 10 行)。但随后它会覆盖第 11 行的 {0: {'best_paying_job': 'superintendent'} 并再次创建一个空字典。然后它在第 12 行将 1: {'other_job': 'social_studies_teacher'}' 分配给 jobs_in_school_district

但是,如果我在每个 for 块(第 7 行和第 11 行)中删除两个 jobs_in_school_district[key] = {},并在“for”语句(新的第 5 行)之前放置一个,如下所示:

jobs_in_school_district[key] = {}

for key in school_districts:
    if (employee['skills'] == school_districts[key]['needs']):
        jobs_in_school_district[key]['best_paying_job'] = 'superintendent'

    if (employee['teaches'] == jobs[key]['needs']):
        jobs_in_school_district[key]['other_job'] = 'social_studies_teacher'

print(jobs_in_school_district)

它只会检查“school_districts”字典中的第一个键,然后停止(我猜它会停止循环,我不知道),所以我明白了:

jobs_in_school_district = {0: {'best_paying_job': 'superintendent'}

(我已经尝试重写了几次,但有时我会得到一个“关键错误”)。

第一个问题:为什么第二个代码块不起作用? 第二个问题:我如何编写代码才能正常工作?

(我不太了解'next'(方法或函数)以及它的作用,所以如果我必须使用它,您能解释一下吗?谢谢)。

【问题讨论】:

  • 你应该在这里阅读如何格式化代码示例,这是不可读的。
  • 除非有办法强制,否则它拒绝这样做。

标签: python dictionary conditional key iteration


【解决方案1】:

如果您将 social_studies 更改为不带下划线的社会研究,则代码将按预期工作。看到这一行:

school_districts = {0: {'needs':  'superintendent', 'grades': 'K-12'}, 
                    1:{'needs': 'social_studies', 'grades': 'K-12'}}

【讨论】:

    【解决方案2】:

    尝试放置

    jobs_in_school_district[key] = {}
    

    在 for 循环之后但在 if 语句之前。

    是的,格式不可读。

    【讨论】:

      【解决方案3】:

      最简单的修复(并回答您的第一个问题):key 未在您最新的 sn-ps 中正确定义,分配必须for 内部,但在if 外部年代:

      for key in school_districts:
          jobs_in_school_district[key] = {}
          if ... etc etc ...
      
          if ... other etc etc ...
      

      最简单的实际上可能是使用“默认字典”而不是普通字典:

      import collections
      jobs_in_school_district = collections.defaultdict(dict)
      

      现在您可以删除对[key] 索引的分配,如果任何给定键首次需要,它将自动为您完成。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-10-14
        • 1970-01-01
        • 1970-01-01
        • 2023-01-31
        • 2015-09-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多