I am not new to programming but I am new to Julia. I have a Julia dictionary object that looks like the following:
Dict{Any,Any}(28.1=>1, 132.0=>2, 110.0=>3)
How can I write code to filter out the values that meet a certain criteria? Like let's say that I want all pairs where the value is >2 or >=2. I'm basically looking for the LINQ equivalent in Julia.
A dictionary in Julia can be created with a pre-defined keyword Dict(). This keyword accepts key-value pairs as arguments and generates a dictionary by defining its data type based on the data type of the key-value pairs. One can also pre-define the data type of the dictionary if the data type of the values is known.
To test for the presence of a key in a dictionary, use haskey or k in keys(dict) .
The values of Dictionary are mutable, or "settable", and can be modified via setindex! . However, just like for Array s, new indices (keys) are never created or rearranged this way.
In Base
, the standard way is to use map
and filter
:
julia> d = Dict{Any,Any}(28.1=>1, 132.0=>2, 110.0=>3);
julia> filter((k, v) -> v > 2, d)
Dict{Any,Any} with 1 entry:
110.0 => 3
If you use DataFrames, there is a LINQ-like interface in DataFramesMeta.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With