gpt4 book ai didi

dictionary - 我如何获得 Julia 字典的大小

转载 作者:行者123 更新时间:2023-12-05 02:01:46 29 4
gpt4 key购买 nike

如何在 julia 中获取字典的大小? size() 抛出错误。

julia> d = Dict(:x => 1, :y => 2)
julia> size(d)
MethodError: no method matching size(::Dict{Symbol,Int64})

最佳答案

使用length()

julia> d = Dict(:x => 1, :y => 2)
julia> length(d)
2

size() 不起作用的原因是 size 用于给出容器的尺寸。来自docs :

size(A::AbstractArray, [dim])

Return a tuple containing the dimensions of A. Optionally you can specify a dimension to just get the length of that dimension.

length(A::AbstractArray)

Return the number of elements in the array, defaults to prod(size(A)).

字典的关键在于它们实际上没有维度。您当然可以将它们表示为一维,但这会忽略字典值可以具有不一定统一的“维度”这一事实。例如,这本字典应该有哪些维度?二?用户可能错误地认为他们可以访问 dict[:a][1]:

julia> dict = Dict(:a => 1, :b => [1, 2])
Dict{Symbol,Any} with 2 entries:
:a => 1
:b => [1, 2]

关于dictionary - 我如何获得 Julia 字典的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66308420/

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