Python:如果满足条件,则循环一个字典并在新字典中创建键/值对
我想将一个字典的值与第二个字典的值进行比较。如果值满足特定条件,我想创建第三个字典,其中的键和值对将根据匹配情况而变化。
这是一个显示我的问题的人为示例。
编辑:对所有返回感到抱歉,但堆栈溢出无法识别单个返回,并且在一行上运行 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
。
但是如果我消除每个中的两个 jobs_in_school_district[key] = {}
for 块(第 7 行和第 11 行),只需在“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”(方法或函数)及其作用,所以如果我必须使用它,你能解释一下吗?谢谢)。
I want to compare the values of one dictionary to the values of a second dictionary. If the values meet certain criteria, I want to create a third dictionary with keys and value pairs that will vary depending on the matches.
Here is a contrived example that shows my problem.
edit: sorry about all the returns, but stack overflow is not recognizing single returns and is running 3-4 lines onto one line, making the code illegible. also, it's not greying out my code as code. don't know why.
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)
This is the value I want to see for 'jobs_in_school_district ':
{0: {'best_paying_job': 'superintendent'},
1: {'other_job': 'social_studies_teacher'}}
This is what I'm getting:
{1: {'other_job': 'social_studies_teacher'}}
I understand what's wrong here. Python is setting jobs_in_school_district
equal to {0: {'best_paying_job': 'superintendent'}
after the first if block (lines 6-8). Then it executes the second if block (line 10). But then it overwrites the {0: {'best_paying_job': 'superintendent'}
at line 11 and creates an empty dict again. Then it assigns 1: {'other_job': 'social_studies_teacher'}' to jobs_in_school_district
at line 12.
But if I eliminate the two jobs_in_school_district[key] = {}
in each of the for blocks (lines 7 and 11) and just put one before the 'for' statement (new line 5) like this:
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)
It will only check the first key in the 'school_districts' dict and then stop (it stops looping I guess, I don't know), so I get this:
jobs_in_school_district = {0: {'best_paying_job': 'superintendent'}
(I've tried re-writing it a few times and sometimes I get a "key error" instead).
First question: why doesn't that second block of code work?
Second question: how do I write code so it does work?
(I don't really understand 'next' (method or function) and what it does, so if I have to use it, could you please explain? Thanks).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的修复(并回答您的第一个问题):您的最新代码片段中未正确定义
key
,但分配必须位于for
内部在if
之外:最简单的实际上可能是使用“默认字典”而不是普通字典:
现在您可以删除对
[key]
索引的分配,它将是当第一次需要任何给定的密钥时,会自动为您完成。Simplest fix (and answer to your first question):
key
is not properly defined in your latest snippets, the assignment must be inside thefor
though outside theif
s:Simplest may actually be to use "default dicts" instead of plain ones:
Now you can remove the assignment to the
[key]
indexing and it will be done for you, automatically, if and when needed for the first time for any given key.尝试放置
在 for 循环之后但 if 语句之前。
是的,格式不可读。
Try placing
after the for loop but before the if statements.
And yea the formatting is unreadable.
如果您将 Social_studies 更改为不带下划线的社会研究,则代码将按您的预期工作。看到这一行:
If you change social_studies to social studies without the underscore the code works as you expected. See this line: