我想使用像HashTable這樣的結構。在Wolfram中是否有類似的結構Mathematica?Wolfram Mathematica中是否有HashTable結構?
回答
更新: Mathematica版本10引入了Association
數據結構(tutorial)。
有許多可能性。如果不需要添加或刪除表中的鍵或更改關聯值,最簡單的方法是使用左側的鍵和右側的值構造一個規則列表手邊,並使用Dispatch
就可以了。
如果您確實需要更改表中的條目,則可以使用符號的DownValues
作爲散列表。這將支持散列表通常使用的所有操作。下面是這樣做的最直接的方法:
(* 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
我會說你可以得到最相似的結構開箱即用是sparse arrays。
我做了Dictionary.m模塊,其中包含:
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]]
];
];
]
忘記: 的ForEach =函數[ { 列表, FUNC }, LEN =長度[表] 對於[i = 1,i <= len,i ++, func [list [[i]]]; ]; ] – Fiard 2011-11-30 16:40:38
你可以編輯你的答案來包含它。 – 2011-11-30 16:58:39
數學10引入了協會,<| k -> v |>
,
<|a -> x, b -> y, c -> z|>
%[b]
y
這基本上是對一系列規則的包裝: 轉換一個協會的規則清單:
Association[{a -> x, b -> y, c -> z}]
<|a -> x, b -> y, c -> z|>
將關聯轉換爲規則列表:
Normal[<|a -> x, b -> y, c -> z|>]
{a -> x, b -> y, c -> z}
- 1. ReleaseHoldAll在Wolfram Mathematica中?
- 2. Eigensystem排序Wolfram Mathematica
- 3. Wolfram Mathematica返回[]奇怪的結果是什麼?
- 4. 在Mathematica/Wolfram語言中,是否有任何方法來檢查表達式是否被評估?
- 5. Wolfram Mathematica中的神經網絡包不是平行的嗎?
- 6. 繪製自己的函數wolfram mathematica
- 7. 將Mathematica項目導入Wolfram Workbench 2.0
- 8. 是否有任何通用的HashTable?
- 9. Mathematica中的結構數據類型?
- 10. 在Wolfram Mathematica中處理大型數據文件的方式
- 11. java是否有「LinkedConcurrentHashMap」數據結構?
- 12. PHP是否有Set數據結構?
- 13. YAML :: Node是否有Mark結構?
- 14. json-array結構是否有效
- 15. Wolfram Mathematica 7爲CS/CEN學生提供什麼?
- 16. 無法集成兩個高斯CDF的減法[Wolfram Mathematica]
- 17. Wolfram Mathematica從多個文件導入數據
- 18. 根據未知常數求解方程wolfram mathematica
- 19. Objective-C - 是否有可能在我的結構中有NSArray?
- 20. 是/ Android是否有任何標準軟件包結構/層次結構實踐?
- 21. 請求成員「」不是一個結構或聯合錯誤hashTable中分離鏈
- 22. Hashtable是否適合存儲資產?
- 23. 如何用相機跟隨物體在Wolfram Mathematica中創建2D(3D)動畫?
- 24. Mathematica中的$ SystemID不正確
- 25. 是否有可能在Java中獲得扁平(unboxed)結構?
- 26. Emacs中是否有函數調用層次結構
- 27. 是否有可能僅檢出Java中cvsclient的目錄結構?
- 28. 是否有可能在C#中「繼承部分結構」?
- 29. 在結構體中的「char」之後是否有零字節?
- 30. C++ Boost中是否有任何回收池結構?
此答案值得多個選票。在我看來,使用開箱即用的結構比構建自己的結構幾乎總是更好。但是'Pillsy'也給出了很好的答案。 – Shredderroy 2013-10-13 19:27:12