如何在Perl中将多键哈希实现为Python中的嵌套字典?
我有一些基本的代码来处理Perl哈希,在这里我可以处理如下元素:$data{“WV2”}{789}{PP1}(或者在赋值中使用该实际文本)……但是我想使用Python字典做类似的事情 下面是几个用Perl和Python编写的简单程序,它们演示了我一直试图复制的内容:- 因此,Perl代码:-如何在Perl中将多键哈希实现为Python中的嵌套字典?,python,perl,dictionary,hash,Python,Perl,Dictionary,Hash,我有一些基本的代码来处理Perl哈希,在这里我可以处理如下元素:$data{“WV2”}{789}{PP1}(或者在赋值中使用该实际文本)……但是我想使用Python字典做类似的事情 下面是几个用Perl和Python编写的简单程序,它们演示了我一直试图复制的内容:- 因此,Perl代码:- # hash.pl use strict; use warnings; use Data::Dumper; my %data = (); my @reg_list = ( "MC1", "CA2",
# hash.pl
use strict;
use warnings;
use Data::Dumper;
my %data = ();
my @reg_list = ( "MC1", "CA2", "WV2" );
my @site_list = ( 123, 456, 391, 287 );
$data{MC1}{4564}{PP}{1} = "-15,-15C";
$data{MC1}{4564}{PP}{2} = "5,5C";
$data{MC1}{4564}{PP}{3} = "-19,-19C";
$data{MC1}{4564}{PP}{4} = "-12,-12C";
printf("---- One:\n");
print Dumper(%data); # Ok, shows the full strucure
printf("---- Two:\n");
print Dumper($data{"MC2"}); # Shows as undef (sensible)
printf("---- Three:\n");
print Dumper($data{"MC1"}); # Ok, showing the key:values for each "site" key
printf("---- Four:\n");
print Dumper($data{"MC1"}{"4564"}); # Ok, shows the actual equality value above
# ---- This works Ok
my %xdata = ();
$xdata{"MC1"}{123}{"PP"} = "-15,-15C";
$xdata{"MC1"}{456}{"PP"} = "5,5C";
$xdata{"MC1"}{391}{"PP"} = "-19,-19C";
$xdata{"MC1"}{287}{"PP"} = "-12,-12C";
printf("---- One:\n");
print Dumper(%xdata); # Ok, shows the full strucure
#pprint.pprint(data["MC2"])
#pprint.pprint(data["MC1"}{391])
# [eof]
…以及Python代码:-
# dict.py
import pprint
import collections
reg_list = [ "MC1", "CA2", "WV2" ]
site_list = [ 123, 456, 391, 287 ]
#data = {}
data = collections.defaultdict(dict) # {}
data["MC1"][123] = "-15,-15C"
data["MC1"][456] = "5,5C"
data["MC1"][391] = "-19,-19C"
data["MC1"][287] = "-12,-12C"
print("---- One:")
pprint.pprint(data) # Ok, shows the full strucure
print("---- Two:")
pprint.pprint(data["MC2"]) # Shows: {} [...Ok, undefined...]
print("---- Three:")
pprint.pprint(data["MC1"]) # Ok, showing the key:values for each "site" key
print("---- Four:")
pprint.pprint(data["MC1"][391]) # Ok, shows the actual equality value above
# ---- Cannot get the following to work
xdata = collections.defaultdict(dict) # {}
xdata["MC1"][123]["PP"] = "-15,-15C" # ERROR: Key error 123
xdata["MC1"][456]["PP"] = "5,5C"
xdata["MC1"][391]["PP"] = "-19,-19C"
xdata["MC1"][287]["PP"] = "-12,-12C"
#pprint.pprint(data["MC2"])
#pprint.pprint(data["MC1"][391])
# [eof]
每个程序的输出如下所示:-
# Perl Output:
---- One:
$VAR1 = 'MC1';
$VAR2 = {
'4564' => {
'PP' => {
'4' => '-12,-12C',
'1' => '-15,-15C',
'3' => '-19,-19C',
'2' => '5,5C'
}
}
};
---- Two:
$VAR1 = undef;
---- Three:
$VAR1 = {
'4564' => {
'PP' => {
'4' => '-12,-12C',
'1' => '-15,-15C',
'3' => '-19,-19C',
'2' => '5,5C'
}
}
};
---- Four:
$VAR1 = {
'PP' => {
'4' => '-12,-12C',
'1' => '-15,-15C',
'3' => '-19,-19C',
'2' => '5,5C'
}
};
---- One:
$VAR1 = 'MC1';
$VAR2 = {
'391' => {
'PP' => '-19,-19C'
},
'456' => {
'PP' => '5,5C'
},
'123' => {
'PP' => '-15,-15C'
},
'287' => {
'PP' => '-12,-12C'
}
};
…从Python中:-
# Python Output:-
---- One:
defaultdict(<class 'dict'>,
{'MC1': {123: '-15,-15C',
287: '-12,-12C',
391: '-19,-19C',
456: '5,5C'}})
---- Two:
{}
---- Three:
{123: '-15,-15C', 287: '-12,-12C', 391: '-19,-19C', 456: '5,5C'}
---- Four:
'-19,-19C'
Traceback (most recent call last):
File "C:\Projects\00-Development\LXQuery\CDB-Review\dict.py", line 30, in <module>
xdata["MC1"][123]["PP"] = "-15,-15C" # ERROR: Key error 123
KeyError: 123
#Python输出:-
----一:
defaultdict(,
{'MC1':{123:'-15,-15C',
287:“-12,-12C”,
391:“-19,-19C”,
456:'5,5C'}})
----二:
{}
----三:
{123:'-15,-15C',287:'-12,-12C',391:'-19,-19C',456:'5,5C'}
----四:
“-19,-19C”
回溯(最近一次呼叫最后一次):
文件“C:\Projects\00 Development\LXQuery\CDB Review\dict.py”,第30行,在
扩展数据[“MC1”][123][“PP”]=“-15,-15C”#错误:键错误123
关键字错误:123
我试图查找有关嵌套字典的信息。。。但我所看到的一切并没有清楚地解释这个概念是如何运作的(无论如何,在我看来)。。。。尤其是在使用的词典有“更深层次”的时候
我写Perl代码已经25年了,但我只是从Python开始
在Windows 10 x64下运行ActiveState Perl v5.16.3、Build 1603和Anaconda Python 3.6.5
非常感谢您的任何想法或建议。Python没有像Perl处理哈希那样自动激活多级字典。在第二级和更深一级,您必须将空的
dict
分配给更高级别的dict
s,然后才能向其添加更多键:
xdata = collections.defaultdict(dict)
xdata["MC1"] = collections.defaultdict(dict)
xdata["MC1"][123]["PP"] = "-15,-15C" # ERROR: Key error 123
xdata["MC1"][456]["PP"] = "5,5C"
xdata["MC1"][391]["PP"] = "-19,-19C"
xdata["MC1"][287]["PP"] = "-12,-12C"
解决问题的简单方法似乎是:-
xdata = collections.defaultdict(dict) # {}
xdata["MC1"][123] = {} # Define the dict before using it
xdata["MC1"][123]["PP"] = "-15,-15C" # Works Ok
…但这仍然意味着,每当我“发现”一个新的“值”时,我必须“手动”定义一个dict。。。废话
尽管与生俱来的抓到你了由于打字错误和(可能)听写内容损坏,似乎是解决这个问题的一个好办法。。。尤其是这些值(无论如何,在我当前的应用程序中)“永远看不到曙光”(它们是在进入我的应用程序之前由机器生成和验证的)。。。因此,一些可能有效的代码可能是:-
class Vividict(dict):
def __missing__(self, key):
value = self[key] = type(self)() # retain local pointer to value
return value # faster to return than
# ...'dict lookup'
ydata = Vividict()
ydata["MC1"][123]["PP"] = "-15,-15C"
ydata["MC1"][456]["PP"] = "5,5C"
ydata["MC1"][391]["PP"] = "-19,-19C"
ydata["MC1"][287]["PP"] = "-12,-12C"
pprint.pprint(ydata) # Ok, shows the full strucure
感谢堆中的建议和指针。问题在于,当找不到密钥时,您正在初始化
defaultdict
以返回(正常)dict
实例,这就是第二维度失败的原因。不标记为重复,因为这不是完全相同的问题,但请查看感谢指针,Selcuk。。我只使用defaultdict,因为我认为它将消除对整个结构进行初始化的需要(正如@mob在我的想法中指出的那样)。。。但看起来我仍然需要这样做,所以defaultdict可能不是最好的选择。显然,还有更多的研究要做。。。