autovivification

How to assign hash['a']['b']= 'c' if hash['a'] doesn't exist?

做~自己de王妃 提交于 2019-12-17 06:38:10
问题 Is there any way simpler than if hash.key?('a') hash['a']['b'] = 'c' else hash['a'] = {} hash['a']['b'] = 'c' end 回答1: The easiest way is to construct your Hash with a block argument: hash = Hash.new { |h, k| h[k] = { } } hash['a']['b'] = 1 hash['a']['c'] = 1 hash['b']['c'] = 1 puts hash.inspect # "{"a"=>{"b"=>1, "c"=>1}, "b"=>{"c"=>1}}" This form for new creates a new empty Hash as the default value. You don't want this: hash = Hash.new({ }) as that will use the exact same hash for all

Specific Dynamic nested Dictionaries, Autovivification implementation

爷,独闯天下 提交于 2019-12-12 04:31:29
问题 I'm trying to implement a nested dictionary structure in a specific manner. I'm reading in a long list of words. These words are eventually going to need to be searched through often and efficiently so this is how I want my dictionary to be set up: I'm trying to make a nested dictionary structure where the first key value is the length of the word, the value is a dict with the key being the first letter of the word and the value is a dict with the key being the second letter of the word and

Why does Autovivification occur with keys() and not %{..}?

流过昼夜 提交于 2019-12-10 03:17:16
问题 This is a subtlety I found with keys() . $ perl -e 'use warnings; use strict; my $d = { "ab" => 1 }; my @e = keys(%{$d->{cd}});' $ perl -e 'use warnings; use strict; my $d = { "ab" => 1 }; my %e = %{$d->{cd}};' Can't use an undefined value as a HASH reference at -e line 1. I am most puzzled as to why the first snippet would not give an dereferencing error. When I use Data::Dumper , it becomes clear that in the first snippet, $d->{cd} , is autovivified to be {} . Why does keys need to

PHP autovivification

為{幸葍}努か 提交于 2019-12-09 15:41:43
问题 Update: My original intention for this question was to determine if PHP actually has this feature. This has been lost in the answers' focus on the scalar issue. Please see this new question instead: "Does PHP have autovivification?" This question is left here for reference. According to Wikipedia, PHP doesn't have autovivification, yet this code works: $test['a']['b'] = 1; $test['a']['c'] = 1; $test['b']['b'] = 1; $test['b']['c'] = 1; var_dump($test); Output: array 'a' => array 'b' => int 1

Python One-Line Tree using defaultdict. How to reduce the number of arguments required?

点点圈 提交于 2019-12-08 04:15:52
问题 I'm using this gist's defaultdict one-line tree. def tree(): return defaultdict(tree) Currently, you must provide a separate [] for every node you want to add. ie: users = tree() users['harold']['username']['hrldcpr'] users['handler']['username']['matthandlersux'] My question is, how would I be able to flatten the input so that I could provide a list to achieve the same result? ie: users = tree() users['harold', 'username', 'hrldcpr'] users['handler', 'username', 'matthandlersux'] Thanks for

Python One-Line Tree using defaultdict. How to reduce the number of arguments required?

独自空忆成欢 提交于 2019-12-07 13:44:31
I'm using this gist's defaultdict one-line tree. def tree(): return defaultdict(tree) Currently, you must provide a separate [] for every node you want to add. ie: users = tree() users['harold']['username']['hrldcpr'] users['handler']['username']['matthandlersux'] My question is, how would I be able to flatten the input so that I could provide a list to achieve the same result? ie: users = tree() users['harold', 'username', 'hrldcpr'] users['handler', 'username', 'matthandlersux'] Thanks for any help! You can simply define a funcion, say insert to create the node by providing a list and tree

How to create nested dictionaries with duplicate keys in python

ε祈祈猫儿з 提交于 2019-12-05 21:13:50
I want to create data structure with nested dictionaries and duplicate keys. A detailed example is: data['State1']['Landon']['abc Area'] = 'BOB' data['State1']['Landon']['abc Area'] = 'SAM' data['State1']['Landon']['xyz Area'] = 'John' data['State2']['New York']['hjk Area'] = 'Ricky' for z in data['State1'].keys() , # I should get list ['Landon', 'Landon', 'Landon'] for y in data['State1']['Landon'].keys() , # I should get list ['abc Area', 'abc Area', 'xyz Area'] Currently to store the data I have used extra counter key data = Autovivification() data[state][city][area][counter] = ID But while

Does PHP have autovivification?

最后都变了- 提交于 2019-12-05 10:53:09
问题 Searching PHP.net for autovivification gives no results. At the time of writing, Wikipedia claims that only Perl has it. There are no clearly definitive results when searching Google for "php autovivification". This PHP code runs fine: $test['a'][4][6]['b'] = "hello world"; var_dump($test); array 'a' => array 4 => array 'b' => array ... Can anyone provide a canonical answer (preferably with references) that PHP does have this feature, and any details such as the version it was introduced in,

Why does Autovivification occur with keys() and not %{..}?

时光总嘲笑我的痴心妄想 提交于 2019-12-05 03:56:41
This is a subtlety I found with keys() . $ perl -e 'use warnings; use strict; my $d = { "ab" => 1 }; my @e = keys(%{$d->{cd}});' $ perl -e 'use warnings; use strict; my $d = { "ab" => 1 }; my %e = %{$d->{cd}};' Can't use an undefined value as a HASH reference at -e line 1. I am most puzzled as to why the first snippet would not give an dereferencing error. When I use Data::Dumper , it becomes clear that in the first snippet, $d->{cd} , is autovivified to be {} . Why does keys need to autovivify? I tried reading the perldoc for it, could not find a satisfying answer. keys does not set an alias

How to implement autovivification for nested dictionary ONLY when assigning values?

大兔子大兔子 提交于 2019-12-04 06:17:07
TL;DR How can I get superkeys to be autovivified in a Python dict when assigning values to subkeys, without also getting them autovivified when checking for subkeys? Background: Normally in Python, setting values in a nested dictionary requires manually ensuring that higher-level keys exist before assigning to their sub-keys. That is, my_dict[1][2] = 3 will not reliably work as intended without first doing something like if 1 not in my_dict: my_dict[1] = {} Now, it is possible to set up a kind of autovivification by making my_dict an instance of a class that overrides __missing__ , as shown e