我想使用像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. Combinator Reduction Wolfram Mathematica
- 3. Eigensystem排序Wolfram Mathematica
- 4. Wolfram Mathematica返回[]奇怪的结果是什么?
- 5. 在Mathematica/Wolfram语言中,是否有任何方法来检查表达式是否被评估?
- 6. Wolfram Mathematica中的神经网络包不是平行的吗?
- 7. 将Mathematica项目导入Wolfram Workbench 2.0
- 8. Wolfram Mathematica 9.0生成垂直表
- 9. 两条线之间的交点,Wolfram Mathematica
- 10. 绘制自己的函数wolfram mathematica
- 11. 是否有任何通用的HashTable?
- 12. Mathematica中的结构数据类型?
- 13. 如何在wolfram mathematica中计算这个积分
- 14. 在Wolfram Mathematica中处理大型数据文件的方式
- 15. 无法在Wolfram Mathematica中生成绘图8
- 16. 在wolfram mathematica中传递列表作为参数
- 17. 转换的HashTable到表格结构
- 18. java是否有“LinkedConcurrentHashMap”数据结构?
- 19. YAML :: Node是否有Mark结构?
- 20. R是否有Set数据结构?
- 21. 是否有处理Timeblocks的结构?
- 22. PHP是否有Set数据结构?
- 23. Coldfusion是否具有“全局”结构
- 24. json-array结构是否有效
- 25. 以下链接结构是否有效?
- 26. 用Mathlink(Wolfram Mathematica)编译REPL(Read-eval-print loop)
- 27. Wolfram Mathematica从多个文件导入数据
- 28. Mathematica对非Wolfram数学字体的支持
- 29. 无法集成两个高斯CDF的减法[Wolfram Mathematica]
- 30. 根据未知常数求解方程wolfram mathematica
此答案值得多个选票。在我看来,使用开箱即用的结构比构建自己的结构几乎总是更好。但是'Pillsy'也给出了很好的答案。 – Shredderroy 2013-10-13 19:27:12