字典双向 [英] Dictionary bidirectional

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

问题描述

我需要一个字典,从''键'获得结果(左),但是

也来自''''(右边),如何获得它?


我知道字典不是双向的,但有没有任何方式

而不使用两个词典?

谢谢提前!

解决方案

7月13日下午4:21,Kless< jonas .... @ googlemail.comwrote:
< blockquote class =post_quotes>
我需要一个字典,从''键'获得结果(左),但

也来自''值''(在如何获得它?


我知道字典不是双向的,但有没有任何方式

而不使用两个词典?


提前致谢!



您想要打印密钥和值吗?


7月13日,4:21 pm,Kless< jonas .... @ googlemail.comwrote:


我需要一个字典,从''键'获得结果(左) ,但

也来自价值(右),如何获得它?


我知道字典不是双向的,但有什么办法

不使用两本字典?


提前致谢!



您需要使用两个词典。这是一个有人'

写的课程,它将它包装成一个类似dict的对象:

http://www.faqts.com/knowledge_base/view.phtml/aid/4376


bukzor:


您需要使用两个词典。这是一个有人'或b
写的类,它将它包装成一个类似dict的对象:
http://www.faqts.com/knowledge_base/view.phtml/aid/4376



它包含如下代码:


尝试:

del self.data [item]

除了KeyError:

传递


例外在python中很有用,但是对于字典,这可能是更快(更短)的b $ b,即使它可以执行两次查找:


如果self.data中的项目:

del self.data [item]


再见,

熊宝宝


I need a dictionary where get the result from a ''key'' (on left), but
also from a ''value'' (on right), how to get it?

I know that dictionaries aren''t bidirectional, but is there any way
without use two dictionaries?
Thanks in advance!

解决方案

On Jul 13, 4:21 pm, Kless <jonas....@googlemail.comwrote:

I need a dictionary where get the result from a ''key'' (on left), but
also from a ''value'' (on right), how to get it?

I know that dictionaries aren''t bidirectional, but is there any way
without use two dictionaries?

Thanks in advance!

You want to print the key as well as the value?


On Jul 13, 4:21 pm, Kless <jonas....@googlemail.comwrote:

I need a dictionary where get the result from a ''key'' (on left), but
also from a ''value'' (on right), how to get it?

I know that dictionaries aren''t bidirectional, but is there any way
without use two dictionaries?

Thanks in advance!

You need to use two dictionaries. Here''s a class that someone''s
written that wraps it up into a single dict-like object for you:

http://www.faqts.com/knowledge_base/view.phtml/aid/4376


bukzor:

You need to use two dictionaries. Here''s a class that someone''s
written that wraps it up into a single dict-like object for you:
http://www.faqts.com/knowledge_base/view.phtml/aid/4376

It contains code like:

try:
del self.data[item]
except KeyError:
pass

Exceptions are useful in python, but with dictionaries this is
probably faster (and shorter), even if it may perform two lookups:

if item in self.data:
del self.data[item]

Bye,
bearophile


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

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