Julia Community 🟣

Vinod V
Vinod V

Posted on

Master Julia Dictionaries with These Easy Recipes

Find out what two dictionaries have in common

a = Dict("x" => 1, "y" => 2, "z" => 3)

b = Dict("w" => 10, "x" => 11, "y" => 2)

println("Common keys:", intersect(keys(a), keys(b)))
println("Keys in a not in b:", setdiff(keys(a), keys(b)))
println("(key,value) pairs in common:", intersect(collect(a), collect(b)))

Output:
Common keys:Set(["x", "y"])
Keys in a not in b:Set(["z"])
(key,value) pairs in common:["y" => 2]
Enter fullscreen mode Exit fullscreen mode

Extracting a subset from a dictionary

prices = Dict(
   "ACME" => 46.23,
   "AAPL" => 612.78,
   "IBM" => 205.55,
   "HPQ" => 37.20,
   "FB" => 10.75,
   "INTL" => 30.56,
   "GS" => 322.3
)

# Make a dictionary of all prices over 100
p100 = Dict([ (key, value) for (key, value) in prices if value > 100 ])

println("All prices over 100")
println(p100)

# Make a dictionary of tech stocks
tech_shares = Set(["AAPL", "IBM", "HPQ", "MSFT","INTL"])
ptech = Dict([ (key, value) for (key, value) in prices if key in tech_shares ])

println("All techs")
println(ptech)

Output:
All prices over 100
Dict("IBM" => 205.55, "AAPL" => 612.78, "GS" => 322.3)
All techs
Dict("HPQ" => 37.2, "IBM" => 205.55, "AAPL" => 612.78, "INTL" => 30.56)
Enter fullscreen mode Exit fullscreen mode

Merging dictionaries in Julia

dict1 = Dict("a" => 1, "b" => 2, "c" => 3)
dict2 = Dict("c" =>10, "e" => 5, "f" => 6)
dict3 = merge(dict1,dict2)
print(dict3)
Output:
Dict("f" => 6, "c" => 10, "e" => 5, "b" => 2, "a" => 1)
Enter fullscreen mode Exit fullscreen mode

Find the N smallest or largest items

portfolio = [
   Dict("name"=> "IBM", "shares"=> 100, "price"=> 91.2),
   Dict("name"=> "AAPL", "shares"=> 50, "price"=> 643.22),
   Dict("name"=> "FB", "shares"=> 150, "price"=> 21.09),
   Dict("name"=> "HPQ", "shares"=> 35, "price"=> 31.75),
   Dict("name"=> "YHOO", "shares"=> 45, "price"=> 16.35),
   Dict("name"=> "ACME", "shares"=> 75, "price"=> 115.65)
]
N = 3
sort(portfolio, by = x -> x["price"])[1:N] #N smallest shares by price
sort(portfolio, by = x -> x["price"],rev=true)[1:N] #N largest by price
Enter fullscreen mode Exit fullscreen mode

Sort a dictionary to find minimum and maximum values

prices = Dict("ACME" => 45.23, "AAPL" => 612.78, "IBM" => 205.55, "HPQ" => 37.20, "FB" => 10.75)

# Find min and max price
min_price = findmin(prices)
max_price = findmax(prices)

println("min price: ", min_price)
println("max price: ", max_price)

println("sorted prices:")
prices_sorted = sort(collect(prices), by=x->x[2])
for (name, price) in prices_sorted
    println("    ", name, " ", price)
end

Output:
min price: (10.75, "FB")
max price: (612.78, "AAPL")
sorted prices:
    FB 10.75
    HPQ 37.2
    ACME 45.23
    IBM 205.55
    AAPL 612.78
Enter fullscreen mode Exit fullscreen mode

Sort a dictionary based on two keys simultaneously

rows = [Dict("fname" =>  "Mohandas", "lname" =>  "Gandhi", "uid" =>  1003),
    Dict("fname" =>  "Jawaharlal", "lname" =>  "Nehru", "uid" =>  1002),
    Dict("fname" =>  "Narasimha", "lname" =>  "Rao", "uid" =>  1001),
    Dict("fname" =>  "Arvind", "lname" =>  "Kejrival", "uid" =>  1004)]



# sort list of Dicts by two keys simultaneously
a = sort(rows, by = x -> (x["lname"], x["fname"]))

print(a)

Output:
Dict("lname" => "Gandhi", "uid" => 1003, "fname" => "Mohandas")
 Dict("lname" => "Kejrival", "uid" => 1004, "fname" => "Arvind")
 Dict("lname" => "Nehru", "uid" => 1002, "fname" => "Jawaharlal")
 Dict("lname" => "Rao", "uid" => 1001, "fname" => "Narasimha")
Enter fullscreen mode Exit fullscreen mode

Top comments (0)