countmap может сверить подсчет элементов в списке:Как совместить Countmap и пропорции в Julia?
import StatsBase: countmap, proportionmap, addcounts!
a = [1,2,3,4,1,2,2,3,1,2,5,7,4,8,4]
b = [1,2,5,3,1,6,1,6,1,2,6,2]
x, y = countmap(a), countmap(b)
[выход]:
(Dict(7=>1,4=>3,2=>4,3=>2,5=>1,8=>1,1=>3),Dict(2=>3,3=>1,5=>1,6=>3,1=>4))
Я мог бы добавить счетчики из исходного списка в countmap
словаря как таковой:
z = addcounts!(x, b)
[выход]:
Dict{Int64,Int64} with 8 entries:
7 => 1
4 => 3
2 => 7
3 => 3
5 => 2
8 => 1
6 => 3
1 => 7
Но если как-то я уже получил подсчитываются словарь, я не мог просто добавить их:
addcounts!(x, y)
[ошибка]:
MethodError: no method matching addcounts!(::Dict{Int64,Int64}, ::Dict{Int64,Int64})
Closest candidates are:
addcounts!{T}(::Dict{T,V}, ::AbstractArray{T,N}) at /Users/liling.tan/.julia/v0.5/StatsBase/src/counts.jl:230
addcounts!{T,W}(::Dict{T,V}, ::AbstractArray{T,N}, ::StatsBase.WeightVec{W,Vec<:AbstractArray{T<:Real,1}}) at /Users/liling.tan/.julia/v0.5/StatsBase/src/counts.jl:237
Ни делал эту работу тоже:
x + y
[погрешность]:
MethodError: no method matching +(::Dict{Int64,Int64}, ::Dict{Int64,Int64})
Closest candidates are:
+(::Any, ::Any, ::Any, ::Any...) at operators.jl:138
Есть ли способ объединить несколько countmap
s?
E.g. в Python:
>>> from collections import Counter
>>> a = [1,2,3,4,1,2,2,3,1,2,5,7,4,8,4]
>>> b = [1,2,5,3,1,6,1,6,1,2,6,2]
>>> x, y = Counter(a), Counter(b)
>>> z = x + y
>>> z
Counter({1: 7, 2: 7, 3: 3, 4: 3, 6: 3, 5: 2, 7: 1, 8: 1})
И y остается неизменным, пока х накапливается? – alvas
Действительно. 'x' не является Dict, но он может быть доступен как таковой. Посмотрите документацию по адресу http://datastructuresjl.readthedocs.io/en/latest/accumulators.html для получения дополнительной информации. –
Спасибо @DanGetz! – alvas