Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query a Julia dictionary for certain values

Tags:

linq

julia

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.

like image 822
Unknown Coder Avatar asked Jul 02 '16 01:07

Unknown Coder


People also ask

How do you access the dictionary 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.

How do you check if a key exists in a dictionary Julia?

To test for the presence of a key in a dictionary, use haskey or k in keys(dict) .

Are Dictionaries mutable in Julia?

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.


1 Answers

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.

like image 98
Fengyang Wang Avatar answered Sep 20 '22 04:09

Fengyang Wang