Main Content

dictionary

将唯一键映射到值的对象

自 R2022b 起. 建议替换 containers.Map.

说明

字典对于在较大数据集中快速查找值来说很有用。字典是将数据存储为的映射,这些值可以使用对应的唯一来访问。每对键和值构成一个条目

创建对象

描述

示例

d = dictionary(keys,values) 使用指定的键和值创建一个字典。生成的字典 d 是一个 1×1 标量对象。如果将多个值赋给同一个键,则只赋这些值中的最后一个值。对现有键的新赋值会覆盖该条目的值。

keysvalues 的大小必须相同,除非 values 是标量,其中 keys 的每个元素成为 values 的一个键。当键和值是数组时,条目的数量等于键-值对组的数量。

字典的类型是根据其条目确定的。字典中的所有键和所有值必须分别具有相同的数据类型,或能够转换为配置的数据类型。如果新条目的某些部分的数据类型不同于已配置的数据类型,则 MATLAB® 会尝试转换。键和值的数据类型不必相同。字符行向量在被赋值为键或值时会转换为字符串标量。

如果不同类型的值包含在一个元胞数组中,则您可以将它们添加到字典中。在对使用元胞作为值的字典执行查找时,将返回元胞数组。使用花括号 ({}) 而不是圆括号可以直接访问元胞数组的内容。 (自 R2023a 起)

d = dictionary(k1,v1,...,kN,vN) 使用指定的键-值对组创建一个字典。如果指定了同一个键的多个实例,则只对最后一个键-值对组赋值。

示例

d = dictionary 创建一个没有键或值的未配置字典。

在创建没有输入的字典时,该字典是未配置的字典,并且没有类型。向未配置的字典中添加条目会为键和值分别指定数据类型。

输入参量

全部展开

键,指定为标量或数组。keys 的各个元素必须为具有相同数据类型或兼容数据类型的标量。

如果 keys 为数组,则每个元素会创建一个新键。如果 keys 包含重复元素,则会赋最后一个重复元素的对应值。

值,指定为标量、数组或元胞数组。values 的各个元素必须为具有相同数据类型的标量。如果值需要是异构或非标量类型,请使用元胞数组。

如果 values 为数组,则每个元素会创建一个新键。如果 keys 为数组,values 为标量,则该值映射到 keys 中的每个键。

键-值对组,键和值参量分别指定,可以为标量和数组。所有键参量必须具有相同的数据类型或兼容的数据类型。所有值参量必须具有相同的数据类型或兼容的数据类型。

用途

描述

使用 dictionary 创建字典 d。然后,您可以使用以下任何语法在特定查询点处计算或更改 d

valueOut = d(keys) 查找对应于 keys 的值。

d(keys) = newValuesnewValues 的元素赋给由 keys 的对应值指定的条目。如果字典中不存在指定的键,则会添加一个新条目。如果将多个值赋给同一个键,则只赋这些值中的最后一个值。对现有键的新赋值会覆盖该条目的值。

d(keys) = [] 从字典中删除与 keys 关联的条目。

valueOut = d{keys} 查找与 keys 关联的值并返回元胞的内容。如果 keys 是数组,则返回对应值的以逗号分隔的列表。如果字典的值配置为不同于元胞的数据类型,则会引发错误。

d{keys} = newValues 将包含 newValues 的元素的元胞赋给由对应键值指定的条目。如果字典的值配置为元胞以外的数据类型,则会引发错误。

对象函数

configureDictionaryCreate dictionary with specified key and value types
insertAdd entries to a dictionary
lookupFind value in dictionary by key
removeRemove dictionary entries
entriesKey-value pairs of dictionary
keys字典的键
valuesValues of dictionary
typesTypes of dictionary keys and values
numEntriesNumber of key-value pairs in dictionary
isConfiguredDetermine if dictionary has types assigned to keys and values
isKeyDetermine if dictionary contains key

示例

全部折叠

创建一个字典来存储不同车辆的车轮数量。

创建一个名称数组和一个对应轮数的数组。

wheels = [1 2 3];
names = ["Monocycle" "Bicycle" "Tricycle"];

创建一个字典,使用名称作为键,使用轮数作为值。

d = dictionary(names,wheels)
d =

  dictionary (string --> double) with 3 entries:

    "Monocycle" --> 1
    "Bicycle"   --> 2
    "Tricycle"  --> 3

通过使用键作为索引来访问字典值。

d("Tricycle")
ans = 3

条目可以通过将新值赋给现有键来修改。

d("Bicycle") = 2.5
d =

  dictionary (string --> double) with 3 entries:

    "Monocycle" --> 1
    "Bicycle"   --> 2.5000
    "Tricycle"  --> 3

通过赋值给键向字典中添加新条目。

d("Car") = 4
d =

  dictionary (string --> double) with 4 entries:

    "Monocycle" --> 1
    "Bicycle"   --> 2.5000
    "Tricycle"  --> 3
    "Car"       --> 4

使用由键和值组成的数组添加多个条目。

names2 = ["Truck" "Motorcycle" "Sixteen-Wheeler"];
wheels2 = [4 2 16];
d(names2) = wheels2
d =

  dictionary (string --> double) with 7 entries:

    "Monocycle"       --> 1
    "Bicycle"         --> 2.5000
    "Tricycle"        --> 3
    "Car"             --> 4
    "Truck"           --> 4
    "Motorcycle"      --> 2
    "Sixteen-Wheeler" --> 16

通过将空数组赋给现有键来删除条目。

d("Truck") = []
d =

  dictionary (string --> double) with 6 entries:

    "Monocycle"       --> 1
    "Bicycle"         --> 2.5000
    "Tricycle"        --> 3
    "Car"             --> 4
    "Motorcycle"      --> 2
    "Sixteen-Wheeler" --> 16

新条目会自动转换以匹配字典的已配置数据类型。如果无法进行转换,MATLAB 会引发错误。

d('Spider-Car') = "8"
d =

  dictionary (string --> double) with 7 entries:

    "Monocycle"       --> 1
    "Bicycle"         --> 2.5000
    "Tricycle"        --> 3
    "Car"             --> 4
    "Motorcycle"      --> 2
    "Sixteen-Wheeler" --> 16
    "Spider-Car"      --> 8

字典值必须具有相同的类型。但不同类型的数据可以作为一个元胞存储在字典中。

创建一个包含各种数据类型的元胞数组和一个由键组成的字符串数组。

myValues = {datetime,@myfun,struct,[1 2 3 4]}
myValues=1×4 cell array
    {[12-Feb-2024 22:37:26]}    {@myfun}    {1x1 struct}    {[1 2 3 4]}

myKeys = ["my birthday" "my favorite function" "a structure" "numeric array"]
myKeys = 1x4 string
    "my birthday"    "my favorite function"    "a structure"    "numeric array"

使用指定的键和值创建一个字典。

d = dictionary(myKeys,myValues)
d =

  dictionary (string --> cell) with 4 entries:

    "my birthday"          --> {[12-Feb-2024 22:37:26]}
    "my favorite function" --> {@myfun}
    "a structure"          --> {1x1 struct}
    "numeric array"        --> {[1 2 3 4]}

使用 values 函数将值提取为一个元胞数组。

values(d)
ans=4×1 cell array
    {[12-Feb-2024 22:37:26]}
    {                @myfun}
    {1x1 struct            }
    {[             1 2 3 4]}

在 R2023a 中,直接使用花括号 ({}) 查找作为值存储的元胞的内容。

d{"numeric array"}
ans = 1×4

     1     2     3     4

同样,任何数据类型的新条目都可以使用花括号 ({}) 插入到具有元胞值的现有字典中。

d{"a new entry"} = table
d =

  dictionary (string --> cell) with 5 entries:

    "my birthday"          --> {[12-Feb-2024 22:37:26]}
    "my favorite function" --> {@myfun}
    "a structure"          --> {1x1 struct}
    "numeric array"        --> {[1 2 3 4]}
    "a new entry"          --> {0x0 table}

在不使用任何输入的情况下调用 dictionary 会创建一个未配置的字典。

使用 configureDictionary 创建一个空的已配置字典。

在 R2023b 之前:指定所需类型的空输入。例如,d = dictionary(string([]),[]).

d = configureDictionary("string","double")
d =

  dictionary (string --> double) with no entries.

只要数据类型匹配或可以转换为已配置字典的数据类型,就可以配置此字典并添加新条目。

d("Unicycle") = 1;
d("Bicycle") = 2;
d("Tricycle") = 3
d =

  dictionary (string --> double) with 3 entries:

    "Unicycle" --> 1
    "Bicycle"  --> 2
    "Tricycle" --> 3

通过在不使用输入的情况下调用 dictionary 来创建一个未配置的字典。

d = dictionary
d =

  dictionary with unset key and value types.

当您向未配置的字典中添加条目时,MATLAB 会将该字典更改为已配置字典。

names = ["Unicycle" "Bicycle" "Tricycle"];
wheels = [1 2 3];
d(wheels) = names
d =

  dictionary (double --> string) with 3 entries:

    1 --> "Unicycle"
    2 --> "Bicycle"
    3 --> "Tricycle"

版本历史记录

在 R2022b 中推出

全部展开