alvas - 4 years ago 176
Python Question

# How to combine countmap and proportionmap in Julia?

The countmap can collate the counts of items in a list:

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

[out]:

``````(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))
``````

I could add the counts from the raw list into the
`countmap`
dictionary as such:

``````z = addcounts!(x, b)
``````

[out]:

``````Dict{Int64,Int64} with 8 entries:
7 => 1
4 => 3
2 => 7
3 => 3
5 => 2
8 => 1
6 => 3
1 => 7
``````

But if somehow I have already got a counted dictionary, I couldn't just add them:

``````addcounts!(x, y)
``````

[error]:

``````MethodError: no method matching addcounts!(::Dict{Int64,Int64}, ::Dict{Int64,Int64})
Closest candidates are:
``````

Neither did this work too:

``````x + y
``````

[error]:

``````MethodError: no method matching +(::Dict{Int64,Int64}, ::Dict{Int64,Int64})
Closest candidates are:
+(::Any, ::Any, ::Any, ::Any...) at operators.jl:138
``````

Is there a way to combine multiple
`countmap`
s ?

E.g. in 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})
``````

``````using DataStructures
Now `x` containts the sum of `x` and `y`.