如何在 Julia 中组合 countmap 和 proportionmap?
How to combine countmap and proportionmap in Julia?
countmap 可以整理列表中项目的计数:
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
?
例如在 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})
基本上问题是对两个字典求和。 Julia 标准库确实提供了这样的功能。
你可以试试 DataStructures.jl。 merge()
函数大致相当于在Python中添加Counters。如果你不想要一个完整的包,手工做应该不难。
正如 Jun Zhang 所建议的那样,DataStructures.jl 提供了累加器类型(a.k.a 计数器)。具体要得到题中的结果:
using DataStructures
x,y = counter(a),counter(b)
push!(x,y) # push! replaces addcounts!
现在 x
包含 x
和 y
的总和。
countmap 可以整理列表中项目的计数:
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
?
例如在 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})
基本上问题是对两个字典求和。 Julia 标准库确实提供了这样的功能。
你可以试试 DataStructures.jl。 merge()
函数大致相当于在Python中添加Counters。如果你不想要一个完整的包,手工做应该不难。
正如 Jun Zhang 所建议的那样,DataStructures.jl 提供了累加器类型(a.k.a 计数器)。具体要得到题中的结果:
using DataStructures
x,y = counter(a),counter(b)
push!(x,y) # push! replaces addcounts!
现在 x
包含 x
和 y
的总和。