朱莉娅中是否有Counter对象?

alv*_*vas 3 containers counter histogram julia

在Python中,可以使用高性能对象对列表中的项目进行计数collections.Counter

>>> from collections import Counter
>>> l = [1,1,2,4,1,5,12,1,51,2,5]
>>> Counter(l)
Counter({1: 4, 2: 2, 5: 2, 4: 1, 12: 1, 51: 1})
Run Code Online (Sandbox Code Playgroud)

我已经在http://docs.julialang.org/en/latest/search.html?q=counter中进行搜索,但似乎找不到计数器对象。

我也看过http://docs.julialang.org/en/latest/stdlib/collections.html,但我也找不到。

我已经尝试过在Julia中使用直方图功能,并且它返回了一波弃用消息:

> l = [1,1,2,4,1,5,12,1,51,2,5]
> hist(l)
Run Code Online (Sandbox Code Playgroud)

[出]:

WARNING: sturges(n) is deprecated, use StatsBase.sturges(n) instead.
 in depwarn(::String, ::Symbol) at ./deprecated.jl:64
 in sturges(::Int64) at ./deprecated.jl:623
 in hist(::Array{Int64,1}) at ./deprecated.jl:646
 in include_string(::String, ::String) at ./loading.jl:441
 in execute_request(::ZMQ.Socket, ::IJulia.Msg) at /Users/liling.tan/.julia/v0.5/IJulia/src/execute_request.jl:175
 in eventloop(::ZMQ.Socket) at /Users/liling.tan/.julia/v0.5/IJulia/src/eventloop.jl:8
 in (::IJulia.##13#19)() at ./task.jl:360
while loading In[65], in expression starting on line 1
WARNING: histrange(...) is deprecated, use StatsBase.histrange(...) instead
 in depwarn(::String, ::Symbol) at ./deprecated.jl:64
 in histrange(::Array{Int64,1}, ::Int64) at ./deprecated.jl:582
 in hist(::Array{Int64,1}, ::Int64) at ./deprecated.jl:645
 in hist(::Array{Int64,1}) at ./deprecated.jl:646
 in include_string(::String, ::String) at ./loading.jl:441
 in execute_request(::ZMQ.Socket, ::IJulia.Msg) at /Users/liling.tan/.julia/v0.5/IJulia/src/execute_request.jl:175
 in eventloop(::ZMQ.Socket) at /Users/liling.tan/.julia/v0.5/IJulia/src/eventloop.jl:8
 in (::IJulia.##13#19)() at ./task.jl:360
while loading In[65], in expression starting on line 1
WARNING: hist(...) and hist!(...) are deprecated. Use fit(Histogram,...) in StatsBase.jl instead.
 in depwarn(::String, ::Symbol) at ./deprecated.jl:64
 in #hist!#994(::Bool, ::Function, ::Array{Int64,1}, ::Array{Int64,1}, ::FloatRange{Float64}) at ./deprecated.jl:629
 in hist(::Array{Int64,1}, ::FloatRange{Float64}) at ./deprecated.jl:644
 in hist(::Array{Int64,1}, ::Int64) at ./deprecated.jl:645
 in hist(::Array{Int64,1}) at ./deprecated.jl:646
 in include_string(::String, ::String) at ./loading.jl:441
 in execute_request(::ZMQ.Socket, ::IJulia.Msg) at /Users/liling.tan/.julia/v0.5/IJulia/src/execute_request.jl:175
 in eventloop(::ZMQ.Socket) at /Users/liling.tan/.julia/v0.5/IJulia/src/eventloop.jl:8
 in (::IJulia.##13#19)() at ./task.jl:360
while loading In[65], in expression starting on line 1

**Is there a Counter object in Julia?**
Run Code Online (Sandbox Code Playgroud)

ken*_*ytm 6

如果使用的是朱莉娅0.5+,直方图功能已被弃用,你应该使用StatsBase.jl模块来代替。警告中也对此进行了描述:

警告:不建议使用hist(...)和hist!(...)。在StatsBase.jl中使用fit(Histogram,...)。

但是,如果您使用StatsBase.jl,则可能countmap更接近您的需求:

julia> import StatsBase: countmap                                                                                                                                             

julia> countmap([1,1,2,4,1,5,12,1,51,2,5])                                                                                                                                    
Dict{Int64,Int64} with 6 entries:
  4  => 1
  2  => 2
  5  => 2
  51 => 1
  12 => 1
  1  => 4
Run Code Online (Sandbox Code Playgroud)

  • @alvas一直在努力缩小Base Julia的大小,因此Base中没有太多功能。放在包装中不会影响性能。 (2认同)