在字典中的字典中,如何模拟 Perl 的自动激活行为?

发布于 2024-09-06 22:29:50 字数 289 浏览 4 评论 0原文

谷歌和在线文档都没有对我的查询提供太多见解,所以我想我应该在这里询问社区。

在 Perl 中,您可以轻松设置哈希的哈希并测试最终密钥,如下所示:

my $hash = {};
$hash{"element1"}{"sub1"}{"subsub1"} = "value1";
if (exists($hash{"element1"}{"sub1"}{"subsub1"})) {
   print "found value\n";
}

Python 中的“最佳实践”等效项是什么?

Both Google and the online docs are not delivering much insight on my query, so I thought I would ask the community here.

In Perl, you can easily setup a hash-of-a-hash-of-a-hash and test the final key like so:

my $hash = {};
$hash{"element1"}{"sub1"}{"subsub1"} = "value1";
if (exists($hash{"element1"}{"sub1"}{"subsub1"})) {
   print "found value\n";
}

What's the 'best-practice' equivalent in Python?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

在风中等你 2024-09-13 22:29:50

最接近的等效项可能如下所示:

import collections

def hasher():
  return collections.defaultdict(hasher)

hash = hasher()
hash['element1']['sub1']['subsub1'] = 'value1'
if 'subsub1' in hash['element1']['sub1']:
  print 'found value'

The closest equivalent is probably something like the following:

import collections

def hasher():
  return collections.defaultdict(hasher)

hash = hasher()
hash['element1']['sub1']['subsub1'] = 'value1'
if 'subsub1' in hash['element1']['sub1']:
  print 'found value'
森林很绿却致人迷途 2024-09-13 22:29:50

至于这是否是 Python 中的最佳实践还有待争论:

hash = {}
hash['element1', 'sub1', 'subsub1'] = 'value'
if ('element1', 'sub1', 'subsub1') in hash:
    print "found value"

但是,如果它适合您,它肯定有效并且非常优雅。

主要缺点是您没有中间访问权限。你不能做:

if ('element1', 'sub1') in hash:
   print "found value"

As to whether this is a best practice in Python is up to debate:

hash = {}
hash['element1', 'sub1', 'subsub1'] = 'value'
if ('element1', 'sub1', 'subsub1') in hash:
    print "found value"

But, it certainly works and is very elegant, if it works for you.

The major drawback is that you don't have intermediate access. You can't do:

if ('element1', 'sub1') in hash:
   print "found value"
岁月苍老的讽刺 2024-09-13 22:29:50
from collections import defaultdict

tree = lambda: defaultdict(tree)

t = tree()

t[1][2][3] = 4
t[1][3][3] = 5
t[1][2]['test'] = 6

来自 维基百科 Autovivification

from collections import defaultdict

tree = lambda: defaultdict(tree)

t = tree()

t[1][2][3] = 4
t[1][3][3] = 5
t[1][2]['test'] = 6

from wikipedia Autovivification

睫毛上残留的泪 2024-09-13 22:29:50

我不知道我是否会得到任何同意,但这就是我通常声明字典的字典的方式:

someObj = {
  'element1': {
    'sub1': {
      'subsub1': 'value1'
    }
  }
}

至于检查元素是否存在,我同意这种方法:

try:
  someObj['element1']['sub1']['subsub1']
except KeyError:
  print('no value found')
else:
  print('found value')

I don't know if I'll get any agreement, but this is how I typically declare dictionaries of dictionaries:

someObj = {
  'element1': {
    'sub1': {
      'subsub1': 'value1'
    }
  }
}

As for checking for the presence of an element, I agree with this approach:

try:
  someObj['element1']['sub1']['subsub1']
except KeyError:
  print('no value found')
else:
  print('found value')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文