我有一些基本代码可以处理perl hash,我可以在其中解决:$ data {$ data {" wv2"} {789} {789} {pp1} {pp1}(或在中使用该实际文本作业)...但是我想使用Python词典做类似的事情。
Perl和Python中的几个简单程序,这些程序说明了我一直在尝试复制的内容,请如下: -
所以,perl代码: -
# 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 0-DevelopmentLXQueryCDB-Reviewdict.py", line 30, in <module>
xdata["MC1"][123]["PP"] = "-15,-15C" # ERROR: Key error 123
KeyError: 123
我已经尝试查找有关嵌套词典的信息...但是我所看的所有内容都没有清楚地解释该概念应该如何工作(无论如何,无论如何)....尤其是当使用的字典有"更深的"级别。
我一直在写perl代码已有25年了,但是才从Python开始。
运行Activestate Perl v5.16.3,构建1603和Anaconda Python 3.6.5 Windows 10 x64。
非常感谢任何想法或建议。
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时,每当我"发现'新的'value'... blah
)尽管有固有的 gotcha!的误导性和(可能的)损坏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
感谢堆的建议和指针。