回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p><a href="http://statsbasejl.readthedocs.io/en/latest/counts.html" rel="nofollow noreferrer">countmap</a>可以整理列表中项目的计数:</p>
<pre><code>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)
</code></pre>
<p>[出来]:</p>
^{pr2}$
<p>我可以将原始列表中的计数添加到<code>countmap</code>字典中:</p>
<pre><code>z = addcounts!(x, b)
</code></pre>
<p>[出来]:</p>
<pre><code>Dict{Int64,Int64} with 8 entries:
7 => 1
4 => 3
2 => 7
3 => 3
5 => 2
8 => 1
6 => 3
1 => 7
</code></pre>
<p>但如果我已经有了一本数过的字典,我就不能把它们加起来:</p>
<pre><code>addcounts!(x, y)
</code></pre>
<p>[错误]:</p>
<pre><code>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
</code></pre>
<p>这也没用:</p>
<pre><code>x + y
</code></pre>
<p>[错误]:</p>
<pre><code>MethodError: no method matching +(::Dict{Int64,Int64}, ::Dict{Int64,Int64})
Closest candidates are:
+(::Any, ::Any, ::Any, ::Any...) at operators.jl:138
</code></pre>
<p><strong>有没有办法合并多个<code>countmap</code>?</strong></p>
<p>例如,在Python中:</p>
<pre><code>>>> 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})
</code></pre>