英文:
Simple command to multiply all values of a dictionary by a float
问题
在Julia中,您可以轻松地将Vector、Matrix或Array中的所有值,比如2,与Float64相乘,方法如下:
a = ones(3, 3)
a *= 2
我想知道是否也可以轻松地对字典进行类似操作,比如Dictionnary{Int,Float64}或Dictionnary{Tuple{Int,Int},Float64}。
我知道可以通过迭代键和值来实现,但我想要像dict *= 2这样“原地”进行操作。这是可能的吗?
英文:
In Julia, you can easily multiply all values of a Vector, Matrix or Array a by a Float64, say 2, by:
a=ones(3,3)
a*=2
I wanted to know if this is also easily achievable for dictionaries, for instance, Dictionnary{Int,Float64} or Dictionnary{Tuple{Int,Int},Float64}
I know it can be done by iterating with for loops on keys and values but I want to do it "in place" like dict*=2. Is it possible?
答案1
得分: 2
这个原地的 map! 操作可能比 replace 快20倍。
测试:
julia> @benchmark map!(x->2x, values(d)) setup=(d = Dict(1:100 .=> rand(100)))
BenchmarkTools.Trial: 10000 samples with 985 evaluations.
Range (min … max): 59.391 ns … 154.315 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 60.508 ns ┊ GC (median): 0.00%
Time (mean ± σ): 61.706 ns ± 4.912 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
█
▃██▆▄▄▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂ ▂
59.4 ns Histogram: frequency by time 86.9 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
与之对比:
julia> @benchmark d_mult = replace(kv -> kv[1] => kv[2]*2, d) setup=(d = Dict(1:100 => rand(100)))
BenchmarkTools.Trial: 10000 samples with 30 evaluations.
Range (min … max): 916.667 ns … 274.520 μs ┊ GC (min … max): 0.00% … 99.13%
Time (median): 1.153 μs ┊ GC (median): 0.00%
Time (mean ± σ): 3.052 μs ± 11.919 μs ┊ GC (mean ± σ): 21.27% ± 5.50%
▆█▅▄▁ ▁▂▂▃▂▄▇▆▃▁ ▂
█████▇▇▆▄▄▁▁▄▁▄▅▅▃▃▁▃▁▁███████████▇▇▆▆▅▅▄▄▅▆▅▅▆▄▄▅▄▃▄▄▅▅▅▆▆▆█ █
917 ns Histogram: log(frequency) by time 7.68 μs <
请注意,这是 Julia 编程语言中的性能测试结果,使用 map! 的性能明显更好,比使用 replace 快得多。
英文:
This in-place map! might be 20X faster than replace.
map!(x->2x, values(d))
Testing:
julia> @benchmark map!(x->2x, values(d)) setup=(d = Dict(1:100 .=> rand(100)))
BenchmarkTools.Trial: 10000 samples with 985 evaluations.
Range (min … max): 59.391 ns … 154.315 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 60.508 ns ┊ GC (median): 0.00%
Time (mean ± σ): 61.706 ns ± 4.912 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
█
▃██▆▄▄▃▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▁▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂ ▂
59.4 ns Histogram: frequency by time 86.9 ns <
Memory estimate: 0 bytes, allocs estimate: 0.
vs.
julia> @benchmark d_mult = replace(kv -> kv[1] => kv[2]*2, d) setup=(d = Dict(1:100 .=> rand(100)))
BenchmarkTools.Trial: 10000 samples with 30 evaluations.
Range (min … max): 916.667 ns … 274.520 μs ┊ GC (min … max): 0.00% … 99.13%
Time (median): 1.153 μs ┊ GC (median): 0.00%
Time (mean ± σ): 3.052 μs ± 11.919 μs ┊ GC (mean ± σ): 21.27% ± 5.50%
▆█▅▄▁ ▁▂▂▃▂▄▇▆▃▁ ▂
█████▇▇▆▄▄▁▁▄▁▄▅▅▃▃▁▃▁▁███████████▇▇▆▆▅▅▄▄▅▆▅▅▆▄▄▅▄▃▄▄▅▅▅▆▆▆█ █
917 ns Histogram: log(frequency) by time 7.68 μs <
Memory estimate: 4.72 KiB, allocs estimate: 5.
答案2
得分: 0
我已经找到一种简短的方法,感谢这个答案:
d_mult = replace(kv -> kv[1] -> kv[2]*2, d)
也许有人会发现更短的方法(d_mult *= 2或类似)。
英文:
I have found a short way thanks to this answer:
d_mult = replace(kv -> kv[1] => kv[2]*2, d)
Maybe someone will find something even shorter (d_mult *= 2 or similar)
答案3
得分: 0
我认为你实际上是在提到类似 Base.Dict{Int,Float64} 的类型。有一个替代的字典实现在 Dictionaries.jl 中,提供了类型 Dictionary。与 Base.Dict 不同的是,这种类型被视为值的可迭代器,而不是键,因此可以像数组一样大多数情况下使用:
julia> dict .+ 1
3个元素的 Dictionary{String,Int64}
"a" │ 2
"b" │ 3
"c" │ 4
英文:
I assume you are actually referring to types like Base.Dict{Int,Float64}. There is an alternative implementation of dictionaries in Dictionaries.jl, providing the type Dictionary. This type, in contrast to Base.Dict, is treated as an iterable over values, not keys, and therefore can be used mostly like an array:
julia> dict .+ 1
3-element Dictionary{String,Int64}
"a" │ 2
"b" │ 3
"c" │ 4
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论