dict.insert() 函数
dict.insert()
函数将键值对插入到字典中,并返回一个新的、已更新的字典。
如果键已存在于字典中,该函数将覆盖现有值。
函数类型签名
(dict: [A:B], key: A, value: B) => [A:B] where A: Comparable
有关更多信息,请参阅 函数类型签名。
参数
dict
(必需) 要更新的字典。
key
(必需) 要插入到字典中的键。必须与字典中现有键的类型相同。
value
(必需) 要插入到字典中的值。必须与字典中现有值的类型相同。
示例
将新的键值对插入到字典中
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"]
此页面是否对您有帮助?
感谢您的反馈!