1, "b" => 2, "c" => 3) > dict2 = Dict("d" => 4, "e" => -6ren">
gpt4 book ai didi

dictionary - 在 Julia 中,如何合并字典?

转载 作者:行者123 更新时间:2023-12-03 06:53:13 25 4
gpt4 key购买 nike

在 Julia 中合并字典的最佳方法是什么?

> dict1 = Dict("a" => 1, "b" => 2, "c" => 3)
> dict2 = Dict("d" => 4, "e" => 5, "f" => 6)
# merge both dicts
> dict3 = dict1 with dict2
> dict3
Dict{ASCIIString,Int64} with 6 entries:
"f" => 6
"c" => 3
"e" => 5
"b" => 2
"a" => 1
"d" => 4

最佳答案

https://docs.julialang.org/en/latest/base/collections/#Base.merge

merge(collection, others...)

Construct a merged collection from the given collections. If necessary, the types of the resulting collection will be promoted to accommodate the types of the merged collections. If the same key is present in another collection, the value for that key will be the value it has in the last collection listed.

julia> merge(dict1,dict2)
Dict{ASCIIString,Int64} with 6 entries:
"f" => 6
"c" => 3
"e" => 5
"b" => 2
"a" => 1
"d" => 4

merge!(collection, others...)
Update collection with pairs from the other collections.

julia> merge!(dict1,dict2)
Dict{ASCIIString,Int64} with 6 entries:
"f" => 6
"c" => 3
"e" => 5
"b" => 2
"a" => 1
"d" => 4

julia> dict1
Dict{ASCIIString,Int64} with 6 entries:
"f" => 6
"c" => 3
"e" => 5
"b" => 2
"a" => 1
"d" => 4

关于dictionary - 在 Julia 中,如何合并字典?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36240589/

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