Python双向映射

编程入门 行业动态 更新时间:2024-10-14 06:19:47
本文介绍了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双向映射

本文发布于:2023-11-16 06:55:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1602008.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:双向   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!