I have some basic code to deal with a Perl hash where I can address the elements like: $data{"WV2"}{789}{PP1} (or use that actual text in an assignment) ...but I would like to do something like that using Python dictionaries.
A couple of simple programs in both Perl and Python that illustrate what I've been trying to replicate follow:-
So, the Perl code:-
# 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]
...and the Python code:-
# 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]
Outputs from each of the programs follow:-
# 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'
}
};
...and from the 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
I've tried to look-up info about Nesting Dictionaries... but everything I've looked at doesn't clearly explain how the concept is supposed to work (to my mind, anyway).... particularly when there are 'deeper' levels of the dictionaries in use.
I've been writing Perl code for ~25 years but am only starting with Python.
Running ActiveState Perl v5.16.3, Build 1603 and Anaconda Python 3.6.5 under Windows 10 x64.
Thanks a lot for any thoughts or suggestions.