2017-02-07 12 views
1

В Python, это можно считать пункты в списке, используя объект высокопроизводительной collections.Counter:Есть ли объект Counter в Julia?

>>> 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}) 

Я поиск в http://docs.julialang.org/en/latest/search.html?q=counter, но я не могу показаться, чтобы найти объект счетчика.

Я также посмотрел http://docs.julialang.org/en/latest/stdlib/collections.html, но я не смог найти его.

Я пробовал функцию гистограммы в Джулию и вернулась волна устаревания сообщений:

> l = [1,1,2,4,1,5,12,1,51,2,5] 
> hist(l) 

[выход]:

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?** 

ответ

2

Если вы используете Юлю 0.5+, гистограмма функции has been deprecated, и вместо этого вы должны использовать модуль StatsBase.jl. Это также описано в предупреждении:

ВНИМАНИЕ: hist (...) и hist! (...) устарели. Используйте fit (гистограмма, ...) в StatsBase.jl вместо этого.

Но если вы используете 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 
+0

Спасибо! Любая идея - 'StatsBase' отдельный пакет, а не часть родной Julia 0.5? – alvas

+1

@alvas Это отдельный пакет, но установка пакета - 'Pkg.add()' прочь. – kennytm

+1

@alvas Было преднамеренное усилие уменьшить размер базы Julia, так много функциональности не находится в Base. Быть в пакете не влияет на производительность. – DNF