dict.insert() 函数
dict.insert() 将一个键值对插入到字典中,并返回一个新的、更新后的字典。
如果字典中已经存在该键,则该函数会覆盖现有的值。
函数类型签名
(dict: [A:B], key: A, value: B) => [A:B] where A: Comparable
有关更多信息,请参见 Function type signatures。
参数
字典
(必填) 要更新的字典。
键
(必填) 用于插入字典的键。 必须与字典中现有键的类型相同。
值
(必填) 要插入字典的值。 必须与字典中现有值的类型相同。
示例
向字典中插入一个新的键值对
import "dict"
d = [1: "foo", 2: "bar"]
dict.insert(dict: d, key: 3, value: "baz")// Returns [1: "foo", 2: "bar", 3: "baz"]
覆盖字典中已有的键值对
import "dict"
d = [1: "foo", 2: "bar"]
dict.insert(dict: d, key: 2, value: "baz")// Returns [1: "foo", 2: "baz"]