简体   繁体   English

如何在Python 3中创建这个复杂的字典?

[英]How can I create this complicated dictionary in Python 3?

I am having a heck of a time trying to create a dictionary for a script I am trying to write that handles different IP addresses. 我有一段时间尝试为要编写的脚本创建字典,以处理不同的IP地址。

My question is this: 我的问题是这样的:

Given d1 and d2 , and assuming d1 and d2 will always have an equal number of key:pair items, how can I create d3 ? 给定d1d2 ,并假设d1d2始终具有相同数量的key:pair项,如何创建d3

d1 = {1:a, 2:b, 3:b, 4:c, 5:c}

d2 = {one:a, two:b, three:b, four:c, five:c}

d3 = {a:[{1:one}], b:[{2:two},{3:three}], c:[{4:four},{5:five}]}

You can see d3 contains keys that are the same as the values from d1 and d2 , and for each key in d3 , it has a value of a list, in which there are more dictionaries with key:pair values that correspond to the original keys from d1 and d2 . 您可以看到d3包含与d1d2的值相同的键,并且对于d3每个键,它都有一个列表值,其中包含更多字典,其中key:pair值与原始键相对应从d1d2

I have been trying to create d3 for a while now but I just can't seem to reason out how to do it. 我已经尝试创建d3一段时间了,但是我似乎无法推理出该怎么做。 Your help is greatly appreciated! 非常感谢您的帮助!

First tip before giving out the answer : 给出答案之前的第一个提示:

reverse_d1 = {}
for k, v in d1.iteritems():
  reverse_d1.set_default(v, []).append(k)

All, thank you for the input. 全部,谢谢您的投入。 I really appreciate it. 我真的很感激。 Unfortunately I realized that the way I currently posed the question, it is completely invalid. 不幸的是,我意识到我目前提出这个问题的方式是完全无效的。 The keys in d2 correspond to subnet masks, which are not all unique, so the way I had it structured is not even a valid dictionary. d2中的键与子网掩码相对应,子网掩码并不是唯一的,因此我构造它的方式甚至都不是有效的字典。

After a lot of fussing, I came up with a working solution: 经过大惊小怪,我想出了一个可行的解决方案:

p = 0
sysNames = ['R3','R2','R1']
d1 = OrderedDict([('200.200.200.2','R3'),('200.200.200.1','R2'),('172.172.172.1','R2'),('172.172.172.100','R1'),('192.168.1.151','R1')])

oid1 = '1.3.6.1.2.1.4.20.1.3.200.200.200.2'
oid2 = '1.3.6.1.2.1.4.20.1.3.200.200.200.1'
oid3 = '1.3.6.1.2.1.4.20.1.3.172.172.172.1'
oid4 = '1.3.6.1.2.1.4.20.1.3.172.172.172.100'
oid5 = '1.3.6.1.2.1.4.20.1.3.192.168.1.151'

d2 = {oid1:'255.255.255.0',oid2:'255.255.255.0',oid3:'255.255.255.0',oid4:'255.255.255.0',oid5:'255.255.255.0'}

d1keys = list(d1.keys())
d1values = list(d1.values())
maskOid = '1.3.6.1.2.1.4.20.1.3.'

d3 = {}

for i in range(len(sysNames)):
    ipMaskList = list()
    numInterfaces = d1values.count(sysNames[i])

    for dummy in range(numInterfaces):
        ipMaskList.append({d1keys[p]:d2.get(maskOid+d1keys[p])})
        p += 1

    d3[sysNames[i]] = ipMaskList

As typical, I'm sure this is a horribly inefficient and convoluted way to accomplish my goal. 通常,我确信这是实现我的目标的极其低效且令人费解的方法。 I am absolutely not a great programmer, and am just happy when things run correctly. 我绝对不是一个优秀的程序员,当事情运行正常时,我只是感到高兴。

Again thank you guys you your help, and if anybody would like to post a more efficient solution, please feel free :) 再次感谢你们的帮助,如果有人想发布更有效的解决方案,请随时:)

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

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