Wolfram Mathematica是否有HashTable结构? [英] Is there HashTable structure in Wolfram Mathematica?

查看:113
本文介绍了Wolfram Mathematica是否有HashTable结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用像HashTable这样的结构。在Wolfram数学中有类似的结构吗?

I want to use a Structure like HashTable. Is there similar structure in Wolfram Mathematica?

推荐答案

更新: Mathematica版本10引入了 Association 数据结构(教程)。

Update: Mathematica version 10 introduced the Association data structure (tutorial).

有很多种可能性。最简单的可能性,如果您不需要从表中添加或删除密钥或更改其关联值,则可以很好地构建一个左侧的键和右侧的值的规则列表右边,并使用 Dispatch

There are a number of possibilities. The easiest possibility, which works well if you don't need to add or delete keys from your table, or change their associated values, is to construct a list of rules with the key on the left-hand side and the value on the right-hand side, and use Dispatch on it.

如果您确实需要更改表中的条目,您可以使用符号的 DownValues 作为哈希表。这将支持常用于散列表的所有操作。这是最简单的方法:

If you do need to change the entries in your table, you can use the DownValues of a symbol as a hash table. This will support all the operations one commonly uses with hash tables. Here's the most straightforward way of doing that:

(* 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

这篇关于Wolfram Mathematica是否有HashTable结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆