访问python中的字典字典 [英] Accessing dictionary of dictionary in python

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

问题描述

  nrec = {'bridge':'xapi1',' current_operations':{},'uuid':'9ae5ca7d-e7d6-7a81-f619-d0ea33efb534','tags':[],'other_config':{'is_guest_installer_network':'true','netmask':'255.255.255.0 ','ip_end':'192.168.128.254','ip_begin':'192.168.128.1'},'name_label':'Guest installer network','VIFs':['OpaqueRef:dff106aa-1a94-8384-1c86-862b47c87fcf '],'allowed_operations':[],'PIFs':[],'name_description':'客户将被分配私人本地IP地址的网络','MTU':'1500','blobs':{} } 

这里你可以看到这个字典里面还有一个字典'other_config': {'is_guest_installer_network':'true','netmask':'255.255.255.0','ip_end':'192.168.128.254','ip_begin':'192.168.128.1'} / p>

我想检查 is_guest_installer_network ==true



我已经做了code> nrec [other_config] [is_guest_installer_network] ==true但问题是某些属性具有这个具有空值或不同值的other_config属性。那么在这种情况下我的解决方案会抛出异常。所以我想以一种有效的方式来做,如:如果is_guest_installer_network包含在字典中,并且值(字符串)为真。

解决方案

如果这是一个配置项,你不需要经常访问(因此你的效率要求是有问题的)。配置一次并忘记它(例如设置 self.is_guest_installer_network = True )。



如果你不能忘记关于它,它将取决于入门在你的字典中的可能性。如果项目丢失的可能性更大,如果您执行以下操作,可能会更好。如果一个项目丢失,你会得到一些快捷方式的行为,另一个config dict只被查找一次(对于存在检查和查找后的值。

  def check_guest_installer_network(nrec)
other_config = nrec.get(other_config,无)
返回other_config不是None和other_config.get('is_guest_installer_network',False)

如果项目更有可能,那么懒惰的尝试/除外方法可能更适合。检查性能,超出实际需要处理的异常时的额外性能成本。

  def check_guest_installer_network(nrec):
try:
return nrec [other_config] ['is_guest_installer_network'] ==true
除了KeyError:
return False
/ pre>

毕竟,如果这个检查确实对整体性能有重大影响,你应该把这个变量放在它比在嵌套字典中更容易访问,例如。将它放入一个全局/类成员变量一次,然后享受廉价便利的检查。



你应该看看 cprofile模块,以验证此查找确实是您的软件的瓶颈,这是值得的优化工作。您应该查看时间模块,为您的问题选择最优秀的解决方案。


Hi in my code there is a dictionary of dictionary.

nrec={'bridge': 'xapi1', 'current_operations': {}, 'uuid': '9ae5ca7d-e7d6-7a81-f619-d0ea33efb534', 'tags': [], 'other_config': {'is_guest_installer_network': 'true', 'netmask': '255.255.255.0', 'ip_end': '192.168.128.254', 'ip_begin': '192.168.128.1'}, 'name_label': 'Guest installer network', 'VIFs': ['OpaqueRef:dff106aa-1a94-8384-1c86-862b47c87fcf'], 'allowed_operations': [], 'PIFs': [], 'name_description': 'Network on which guests will get assigned a private local IP address', 'MTU': '1500', 'blobs': {}}

Here you can see inside this dictionary one more dictionary 'other_config': {'is_guest_installer_network': 'true', 'netmask': '255.255.255.0', 'ip_end': '192.168.128.254', 'ip_begin': '192.168.128.1'} is there.

I want to check is_guest_installer_network=="true"

I have done nrec["other_config"]["is_guest_installer_network"]== "true" but the problem is some attribute have this other_config property with either empty value or different value. Then in this case my solution will throw exception. So i want to do it in a efficient way like If is_guest_installer_network is consists in the dictionary and the value (string) is true or not.

解决方案

If this is a config item, you shouldn't need to access it very often (thus your efficiency requirement would be questionable). Configure once and forget about it (e.g. set self.is_guest_installer_network = True).

If you can't forget about it, it would depend on the likelihood of the entry being present in your dictionary. If it's more likely that the item is missing it would probably be better if you do something like the following. You get some shortcut behavior if an item misses, the other config dict is looked up only once (for the existance check and for the value following lookup.

def check_guest_installer_network(nrec):
  other_config = nrec.get("other_config", None)
  return other_config is not None and other_config.get('is_guest_installer_network', False)

If it's more likely that the item is there, the lazy try/except approach could be better suited. As the saved check performance, would outweigh the additional performance cost when the exception actually needs to be handled.

def check_guest_installer_network(nrec):
  try:
    return nrec["other_config"]['is_guest_installer_network'] == "true"
  except KeyError:
    return False

After all, if this check indeed has a significant impact on the overall performance, you should put this variable somewhere it is better accessible, than in a nested dictionary, e.g. put it into a global/class member variable once, and enjoy the cheap and easy checks afterwards.

You should have a look at the cprofile module to verify that this lookup is indeed the bottleneck of your software, that is worth the optimization effort. And You should look at the timeit module to choose the most performant solution for your problem.

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

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