gpt4 book ai didi

dictionary - 数组到 julia 中的元组

转载 作者:行者123 更新时间:2023-12-04 22:19:30 25 4
gpt4 key购买 nike

Julia 的新手,很困惑。

这是一个数组:

array=["a","b",1]

我定义了一个字典
dict=Dict()
dict["a","b"]=1

我想使用“数组”来定义字典
dict2 = Dict()
dict2[array[1:2]]=1

但它们不一样,
julia> dict
Dict{Any,Any} with 1 entry:
("a","b") => 1

julia> dict2
Dict{Any,Any} with 1 entry:
Any["a","b"] => 1

如何使用“array”生成“dict”而不是“dict2”?谢谢

最佳答案

您可以在进行赋值时使用 splat 运算符:

julia> dict = Dict()
Dict{Any,Any} with 0 entries

julia> dict[array[1:2]...] = 1
1

julia> dict
Dict{Any,Any} with 1 entry:
("a","b") => 1

注意:您可以在 Dict 中指定类型,这样可以防止这些类型的错误:
dict = Dict{Tuple{String, String}, Int}()

关于dictionary - 数组到 julia 中的元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37489970/

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