gpt4 book ai didi

julia - 如何向现有字典添加新的键值对?

转载 作者:行者123 更新时间:2023-12-02 22:30:32 31 4
gpt4 key购买 nike

我有一些已经存在的字典。我想添加一个新的键值对,但我不想从头开始创建一个新的字典。

如何将新的键值对添加到 Julia 中的现有字典中?

最佳答案

Julia 使用常见的 dict[key] = value 语法来设置键值对:

julia> dict = Dict(1 => "one")
Dict{Int64,String} with 1 entry:
1 => "one"

julia> dict[2] = "two"
"two"

julia> dict
Dict{Int64,String} with 2 entries:
2 => "two"
1 => "one"

如果键已存在,相同的语法将覆盖键值对:

julia> dict
Dict{Int64,String} with 2 entries:
2 => "two"
1 => "one"

julia> dict[1] = "foo"
"foo"

julia> dict
Dict{Int64,String} with 2 entries:
2 => "two"
1 => "foo"

dict[key] = valuesetindex! 的语法称呼。虽然不常见,但您可以直接调用 setindex!:

julia> dict = Dict(1 => "one")
Dict{Int64,String} with 1 entry:
1 => "one"

julia> setindex!(dict, "two", 2)
Dict{Int64,String} with 2 entries:
2 => "two"
1 => "one"

关于julia - 如何向现有字典添加新的键值对?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59699284/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com