Czy w Wolfram Mathematica istnieje struktura Hasztagowa?
Chcę użyć struktury takiej jak HashTable. Czy istnieje podobna struktura w Wolfram Mathematica ?
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
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łka są rzadkimi tablicami .
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ź:
Zawiera poręczną funkcję do pobierania kluczy z tabeli hash.
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]]
];
];
]
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}
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