使用字典映射数据
containers.Map.字典是一种数据结构体,可在不同类型的数据之间创建关联。字典将数据存储为值,可使用对应的唯一键访问这些值。键和值可以使用不同数据类型,每个键-值对组是一个条目。
字典的基本功能是链接两个相关联的数据集,以便可以使用一个数据集的元素来查找另一个数据集的对应元素。此操作称为查找。无论字典有多少条目,字典都能提供一致的性能。
此示例说明如何创建一个字典并修改其条目。然后它展示字典如何处理数据类型以及如何存储不同数据类型。
创建字典
例如,使用产品作为键、价格作为值创建一个字典。字典输出指示键和值的数据类型。
Products = ["Tomato" "Carrot" "Mango" "Mushroom"]; Prices = [1 .5 2.50 1.99]; d = dictionary(Products,Prices)
d =
dictionary (string ⟼ double) with 4 entries:
"Tomato" ⟼ 1
"Carrot" ⟼ 0.5000
"Mango" ⟼ 2.5000
"Mushroom" ⟼ 1.9900
现在,执行查找以找出胡萝卜的价格。可以使用关联的键查找字典中的任何价格。
d("Carrot")ans = 0.5000
修改字典
要将新条目插入字典中,您可以使用等号 (=) 将值映射到新键。以下命令添加键 "Potato",其值为价格 0.75。
d("Potato") = 0.75d =
dictionary (string ⟼ double) with 5 entries:
"Tomato" ⟼ 1
"Carrot" ⟼ 0.5000
"Mango" ⟼ 2.5000
"Mushroom" ⟼ 1.9900
"Potato" ⟼ 0.7500
您可以通过将新值映射到现有键来更改条目。以下命令将 "Tomato" 的价格更改为 1.25。
d("Tomato") = 1.25d =
dictionary (string ⟼ double) with 5 entries:
"Tomato" ⟼ 1.2500
"Carrot" ⟼ 0.5000
"Mango" ⟼ 2.5000
"Mushroom" ⟼ 1.9900
"Potato" ⟼ 0.7500
通过将键映射到空数组 ([]) 来删除条目。以下命令删除 "Mango" 的条目。
d("Mango") = []d =
dictionary (string ⟼ double) with 4 entries:
"Tomato" ⟼ 1.2500
"Carrot" ⟼ 0.5000
"Mushroom" ⟼ 1.9900
"Potato" ⟼ 0.7500
任何先前的操作都可以向量化,而不是一次处理一个条目。例如,以下命令为 "Celery" 和 "Grapes" 添加两个具有关联价格的新条目。
d(["Celery" "Grapes"]) = [0.50 1.95]
d =
dictionary (string ⟼ double) with 6 entries:
"Tomato" ⟼ 1.2500
"Carrot" ⟼ 0.5000
"Mushroom" ⟼ 1.9900
"Potato" ⟼ 0.7500
"Celery" ⟼ 0.5000
"Grapes" ⟼ 1.9500
字典和数据类型
字典键和值几乎可以采用任何数据类型,字典根据其条目进行定型。一旦分配了数据类型,字典就得以配置。
键和值的数据类型不必相同。不过,字典中的所有键和所有值必须分别具有相同的数据类型,或能够转换为配置的数据类型。
如果插入的新条目与字典的配置数据类型不匹配,则 MATLAB® 会尝试转换数据类型以匹配配置。
例如,创建一个接受字符串形式的键和值的字典。然后添加一个包含数值的条目。MATLAB 将该值转换为字符串。
d = dictionary("hello","world")
d =
dictionary (string ⟼ string) with 1 entry:
"hello" ⟼ "world"
d("newKey") = 1d =
dictionary (string ⟼ string) with 2 entries:
"hello" ⟼ "world"
"newKey" ⟼ "1"
isstring(d("newKey"))ans = logical
1
在字典中存储不同数据类型
对于键和值,字典要求所有条目分别共享相同的数据类型。不过,通过将数据放在元胞数组中,您可以在一个字典中存储多种数据类型。元胞数组的每个元素都可以包含任何类型或大小的数据。这种方法可满足字典类型要求,因为所有值都是元胞数组。
创建一个包含各种数据类型值的元胞数组,然后创建一个由键组成的字符串数组。
myCell = {datetime,@myfun,struct,[1 2 3 4]}myCell=1×4 cell array
{[09-Aug-2025 12:21:27]} {@myfun} {1×1 struct} {[1 2 3 4]}
names = ["my birthday" "my favorite function" "a structure" "numeric array"]
names = 1×4 string
"my birthday" "my favorite function" "a structure" "numeric array"
使用指定的键和值创建一个字典。
d = dictionary(names,myCell)
d =
dictionary (string ⟼ cell) with 4 entries:
"my birthday" ⟼ {[09-Aug-2025 12:21:27]}
"my favorite function" ⟼ {@myfun}
"a structure" ⟼ {1×1 struct}
"numeric array" ⟼ {[1 2 3 4]}
在 R2023a 中,直接使用花括号 ({}) 查找作为值存储的元胞的内容。
d{"numeric array"}ans = 1×4
1 2 3 4
同样,任何数据类型的新条目都可以使用花括号 ({}) 插入到具有元胞值的现有字典中。
d{"a new entry"} = tabled =
dictionary (string ⟼ cell) with 5 entries:
"my birthday" ⟼ {[09-Aug-2025 12:21:27]}
"my favorite function" ⟼ {@myfun}
"a structure" ⟼ {1×1 struct}
"numeric array" ⟼ {[1 2 3 4]}
"a new entry" ⟼ {0×0 table}
数据类型限制
字典支持条目采用几乎任何数据类型,但存在一些限制。某些数据类型(如 struct)可作为条目的一部分接受,但不支持对具有不同字段的结构体进行向量化运算。对于无法串联的类型,不支持向量化查找。
字典不接受键或值采用以下数据类型:
表
tall 数组
分布式数组
graph和digraph对象
未配置的字典
当您创建一个没有任何条目的字典时,它是未配置的字典,并且没有给键和值分配数据类型。
d = dictionary
d = dictionary with unset key and value types.
isConfigured(d)
ans = logical
0
使用 configureDictionary 创建一个没有条目的已配置字典。
d = configureDictionary("string","double")
d = dictionary (string ⟼ double) with no entries.
在 R2023b 之前通过创建预期数据类型的 empty 输入,可以配置字典而不添加任何条目。例如,dictionary(string.empty,struct.empty)。
另请参阅
dictionary | keyHash | keyMatch