Python双向映射 [英] Python bidirectional mapping

查看:79
本文介绍了Python双向映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定要寻找的是什么?因此,如果找不到其他地方的问题,我深表歉意.简而言之,我正在编写将直接与Linux内核接口的python代码.从包含头文件中获取所需的值并将其写入我的源代码很容易:

I'm not sure what to call what I'm looking for; so if I failed to find this question else where, I apologize. In short, I am writing python code that will interface directly with the Linux kernel. Its easy to get the required values from include header files and write them in to my source:

IFA_UNSPEC     =  0
IFA_ADDRESS    =  1
IFA_LOCAL      =  2
IFA_LABEL      =  3
IFA_BROADCAST  =  4
IFA_ANYCAST    =  5
IFA_CACHEINFO  =  6
IFA_MULTICAST  =  7

在构造要发送到内核的结构时,很容易使用这些值.但是,它们几乎无助于解决内核响应中的值.

Its easy to use these values when constructing structs to send to the kernel. However, they are of almost no help to resolve the values in the responses from the kernel.

如果将值放入dict中,则必须扫描dict中的所有值,以从我假定的内核中查找每个结构中每个项目的键.必须有一种更简单,更有效的方法.

If I put the values in to dict I would have to scan all the values in the dict to look up keys for each item in each struct from the kernel I presume. There must be a simpler, more efficient way.

您将如何做? (如有疑问,可随时为该问题重新命名)

How would you do it? (feel free to retitle the question if its way off)

推荐答案

如果要使用两个字典,则可以尝试创建反向字典:

If you want to use two dicts, you can try this to create the inverted dict:

b = {v: k for k, v in a.iteritems()}

这篇关于Python双向映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆