简体   繁体   English

嵌套循环的python字典理解

[英]python dictionary of dictionaries comprehension with nested loops

I am starting to understand and implement dictionary comperhension. 我开始理解和实现字典理解。 I am trying to build a dictionary with nested dictionaries like this: 我试图用这样的嵌套字典来构建字典:

{serviceA:{ip_server1: flag, ip_server2:flag...}, serviceB{ip_server1: flag, ip_server_2: flag...etc}} {serviceA:{ip_server1:标志,ip_server2:标志...},serviceB {ip_server1:标志,ip_server_2:标志...等}}

I am having problems to build it as when getting the result It just throws me tha last element. 当获得结果时,我在构建它时遇到了问题。它只是把我丢掉了。 I have tried many ways and all lead the same way. 我尝试了很多方法,并且都采用相同的方法。 Here is the code I used : 这是我使用的代码:

DICTIONARY 1: 字典1:

services_dict = {key_service: value for key_service,value in config_file.iteritems() if key_service.startswith("s-")}
#It returns a dictionary in this form {service1: [list_of_flags]}

Then I have an external list that has the server IPs 然后我有一个包含服务器IP的外部列表

server_list = [S1, S2, S3, S4..etc] server_list = [S1,S2,S3,S4..etc]

After this I want to use dic comprehension to build the complete dictionary: 之后,我想使用dic comprehension构建完整的字典:

new_services_dict = { key1: {key2:fl} for key1, flag_list in corrected_dict.items() for key2, fl in zip(server_list, flag_list)}

The problem is that it only returns the last element of the ip list (1 element only) 问题在于它仅返回ip列表的最后一个元素(仅1个元素)

{'ServiceA': {'last_server_ip_in_list': 'Last_flag_in_list'}} {'ServiceA':{'last_server_ip_in_list':'Last_flag_in_list'}}

I am not sure why it is not appending new values and just take sthe last item in the iteration. 我不确定为什么不附加新值,而只是在迭代中保留最后一项。

Any help would be greatly appreciated. 任何帮助将不胜感激。 I have already tried many ways and i am not able to find out which part of the iteraiton i am missing. 我已经尝试了许多方法,但无法找出我缺少的迭代部分。

UPDATED INFO 更新信息

As requested in the coment below here is some sample data to clarify the question: Let say we have to map a list of students that I have separated from a dictionary that contains the college subjects with the grades: 按照下面的评论,这里有一些示例数据来澄清这个问题:假设我们必须映射我与包含年级的大学科目的词典分开的学生列表:

services_dict = {math:['A','B','C','D'], Databases:['B','C','D', 'A']} students list = ['Jhon','Michael','Leslie','Lorraine'] services_dict = {数学:['A','B','C','D'],数据库:['B','C','D','A']}学生名单= ['Jhon' ,“迈克尔”,“莱斯利”,“洛林”]

The grades are listed respectively from the student list, so that is not the problem (on which grade belongs to whom). 成绩分别从学生列表中列出,因此这不是问题(哪个成绩属于谁)。

What is intended it to get a dictionary in the form: 它是如何获得以下形式的字典的:

{'math':{'Jhon':'A', 'Michael':'B', 'Leslie':'C', 'Lorraine':'D'}, Databases:{'Jhon':'B', 'Michael':'C', 'Leslie':'D', 'Lorraine':'A'}} {'math':{'Jhon':'A','Michael':'B','Leslie':'C','Lorraine':'D'},数据库:{'Jhon':'B', 'Michael':'C','Leslie':'D','Lorraine':'A'}}

The problem i am getting is that new_services_dict is only returning: 我得到的问题是new_services_dict只返回:

{'math:{'Lorraine':'D'} {'数学:{'洛林':'D'}

I hope this is clearer than the explanation above. 我希望这比上面的解释更清楚。

Well for starters, it looks like all the keys in new_services_dict will be the same since you have a nested for loop and not a nested dictionary comprehension, which I am assuming is what you want. 对于初学者来说, new_services_dict所有键看起来都是一样的,因为您有一个嵌套的for循环,而不是嵌套的字典理解,我想这就是您想要的。

For example, 例如,

>>>{k1:{k2:v} for k1 in ['a','b'] for k2,v in zip(['aa','bb'],[1,2])}
{'b': {'bb': 2}, 'a': {'bb': 2}} # dictionaries are unordered

Here k1 takes value of 'a' then pauses until k2,v for loop iterates through all of the items before resuming. 在此, k1取值为'a'然后暂停,直到k2,v for循环遍历所有项目,然后再继续。 This just keeps reassigning k1 to {'bb':2} since that's the last item in the k2 iteration. 这只是将k1重新分配给{'bb':2}因为那是k2迭代中的最后一项。 This is why all of your values are probably coming out the same. 这就是为什么您的所有价值观可能都一样的原因。

What you probably want is a nested dictionary comprehension that would look like this: 您可能想要的是一个嵌套的字典理解,如下所示:

>>>{k1:{k2:v for k2,v in zip(['aa','bb'],[1,2])} for k1 in ['a','b']}
{'b': {'aa': 1, 'bb': 2}, 'a': {'aa': 1, 'bb': 2}}

Here, k1 takes the value of 'a' then pauses until the nested comprehension runs. 在这里, k1取值'a'然后暂停直到嵌套的理解运行。

Check this out for more information and better explanation on nested comprehensions . 请查看此内容,以获取有关嵌套理解的更多信息和更好的解释。 This is for list comprehensions, but the same logic applies. 这是用于列表理解的,但是适用相同的逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM