Czy w Wolfram Mathematica istnieje struktura Hasztagowa?

Chcę użyć struktury takiej jak HashTable. Czy istnieje podobna struktura w Wolfram Mathematica ?

Author: JayC, 2009-09-08

5 answers

Aktualizacja: Mathematica w wersji 10 wprowadziła Association struktura danych (tutorial ).


Istnieje wiele możliwości. Najprostszą możliwością, która działa dobrze, jeśli nie musisz dodawać lub usuwać kluczy z tabeli lub zmieniać powiązanych z nimi wartości, jest skonstruowanie listy reguł z kluczem po lewej stronie i wartością po prawej stronie i użycie Dispatch na niej.

Jeśli musisz zmienić wpisy w tabeli, możesz użyć DownValues symbolu jako tabeli hash. Będzie to obsługiwać wszystkie operacje powszechnie używane z tabel hash. Oto najprostszy sposób na zrobienie tego:

(* Set some values in your table.*) 
In[1]:=  table[a] = foo; table[b] = bar; table[c] = baz;

(* Test whether some keys are present. *)
In[2]:=  {ValueQ[table[a]], ValueQ[table[d]]}
Out[2]:= {True, False}

(* Get a list of all keys and values, as delayed rules. *)
In[3]:=  DownValues[table]
Out[3]:= {HoldPattern[table[a]] :> foo, HoldPattern[table[b]] :> bar,
HoldPattern[table[c]] :> baz}

(* Remove a key from your table. *)
In[4]:=  Unset[table[b]]; ValueQ[table[b]]
Out[4]:= False
 22
Author: Pillsy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-05 12:51:06

Powiedziałbym, że najbardziej podobną strukturę można uzyskać z pudełkarzadkimi tablicami .

 7
Author: João Silva,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-05-13 04:04:45

Zgadzam się z Pillsy, ale Zobacz też tę odpowiedź:

Mathematica Downvalue LHS

Zawiera poręczną funkcję do pobierania kluczy z tabeli hash.

 5
Author: dreeves,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 11:55:06

Zrobiłem Słownik.moduł m, który zawierał:

DictHasKey = Function[
    {
        dict,
        key
    },
    ValueQ[dict[key]]
]

DictAddKey = Function[
    {
        dict,
        key,
        value
    },
    If[
        DictHasKey[dict,key],
        Print["Warning, Dictionary already has key " <> ToString[key]]
    ];
    dict[key] = value;
]

DictKeys = Function[
    {
        dict
    },
    res = {};
    ForEach[DownValues[dict], Function[{dictKeyDescr},
        res = Append[res, ((dictKeyDescr[[1]]) /. dict -> neverUsedSymbolWhatever)[[1, 1]]];
    ]];
    res
]

DictValues = Function[
    {
        dict
    },
    res = {};
    ForEach[DownValues[dict], Function[{dictKeyDescr},
        res = Append[res, dictKeyDescr[[2]]];
    ]];
    res
]

DictKeyValuePairs = Function[
    {
        dict
    },
    res = {};
    ForEach[DownValues[dict], Function[{dictKeyDescr},
        res = Append[res, {((dictKeyDescr[[1]]) /. dict -> neverUsedSymbolWhatever)[[1, 1]], dictKeyDescr[[2]]}];
    ]];
    res
]

ForEach = Function[
    {
        list, 
        func 
    }, 
    len = Length[list]; 
    For[i = 1, i <= len, i++, 
        func[
            list[[i]]
        ]; 
    ]; 
]
 4
Author: Fiard,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-11-30 17:27:27

Mathematica 10 wprowadza skojarzenie, <| k -> v |>,

<|a -> x, b -> y, c -> z|>
%[b]
y

Który jest w zasadzie opakowaniem listy reguł: Konwertuj listę reguł na asocjację:

Association[{a -> x, b -> y, c -> z}]
<|a -> x, b -> y, c -> z|>

Konwertuj asocjację na listę reguł:

Normal[<|a -> x, b -> y, c -> z|>]
{a -> x, b -> y, c -> z}
 3
Author: masterxilo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-03-05 12:47:38