📜  朱莉娅字典

📅  最后修改于: 2021-11-25 04:36:44             🧑  作者: Mango

Julia 中的字典是键值对的集合,其中字典中的每个值都可以通过其键进行访问。这些键值对不必是相同的数据类型,这意味着字符串类型的键可以保存任何类型的值,如整数、字符串、浮点数等。字典的键永远不会相同,每个键必须是唯一的.这不适用于值,根据需要,值可以相同。默认情况下,字典是一个无序的数据集合,即它不维护插入键的顺序。

字典更像是一个数组,但在字典中,索引可以是任何类型,而在数组中,索引只能是整数。
字典中的每个键都映射到一个值。因为键必须是唯一的,两个键可以映射到两个相同的值,但两个不同的值不能映射到一个键。

句法:

创建字典

Julia 中的字典可以使用预定义的关键字 Dict() 创建。该关键字接受键值对作为参数,并通过基于键值对的数据类型定义其数据类型来生成字典。如果值的数据类型已知,还可以预先定义字典的数据类型。这可以通过在Dict关键字之后的大括号内定义数据类型来完成。这个具有预定义数据类型的字典称为Typed Dictionary

句法:

Dictionary_name = Dict{Key_datatype, Value_datatype}("Key1" => value1, "Key2" => value2, ...)

例子:

# Julia program to illustrate 
# the use of Dictionary
  
# Creating an Empty dictionary
Dict1 = Dict()
println("Empty Dictionary = ", Dict1)
  
# Creating an Untyped Dictionary
Dict2 = Dict("a" => 1, "b" => 2, "c" => 3)
println("\nUntyped Dictionary = ", Dict2)
  
# Creating a Typed Dictionary
Dict3 = Dict{String, Integer}("a" => 10, "c" => 20)
println("\nTyped Dictionary = ", Dict3)

输出:
字典-输出-01

从字典访问元素

可以使用字典的键访问字典中的元素。这些键在字典中是唯一的,因此每个键都有一个值。也可以使用 for 循环访问键值对。

句法:

Dictionary_name[key_name]
or
Dictionary_name[:key_name]

例子:

# Julia program to illustrate 
# the use of Dictionary
  
# Creating a Dictionary with String keys
Dict1 = Dict("a" => 1, "b" => 2, "c" => "Hello")
  
# Accessing dictionary values using keys
println(Dict1["b"])
println(Dict1["c"])
  
# Creating a Dictionary with Integer keys
Dict2 = Dict(1 => 10, 2 => 20, 3 => "Geeks")
println(Dict2[1])
println(Dict2[3])
  
# Creating a Dictionary with Symbols
Dict3 = Dict(:a => 1, :b => "one")
println(Dict3[:b])

输出:
字典-输出-02 get()函数:
Julia 提供了一个预定义的函数来访问字典的元素,称为get()函数。该函数接受 3 个参数:字典名称、键和一个默认值,如果找不到键则打印。
句法:

get(Dictionary_name, Key_name, Default Value)

例子:

# Julia program to illustrate 
# the use of Dictionary
  
# Creating a Dictionary with mixed-typed keys
Dict1 = Dict("a" => 1, "b" => 2, "c" => "Hello", 4 => 10)
  
# Accessing using get() function
  
# Passing '0' as default value 
println(get(Dict1, "b", 0))
  
# Passing String as Default value
println(get(Dict1, "d", "Sorry, no such key"))

输出:
字典-输出-03

从字典访问键和值:

Julia 中的字典允许一次访问所有键和所有值。这可以通过使用预定义的关键字keysvalues

句法:

Keys = keys(Dictionary_name)
Values = values(Dictionary_name)

例子:

# Julia program to illustrate 
# the use of Dictionary
  
# Creating a Dictionary with mixed-typed keys
Dict1 = Dict("a" => 1, "b" => 2, "c" => "Hello", 4 => 10)
  
# Accessing all the Keys
# using 'keys' keyword
Keys = keys(Dict1)
println("Keys = ", Keys)
  
# Accessing all the Values
# using 'values' keyword
Values = values(Dict1)
println("Values = ", Values)

输出:
字典-输出-04
打印键值对:
使用for循环,可以一次打印字典的所有键值对。这是通过迭代字典的每个键然后访问该键的相应值来完成的。

示例 1:使用 Dictionary 作为可迭代对象

# Julia program to illustrate 
# the use of Dictionary
  
# Creating a Dictionary with mixed-typed keys
Dict1 = Dict("a" => 1, "b" => 2, "c" => "Hello", 4 => 10)
  
# Printing key-value pair using
# Dictionary as an iterable object
for i in Dict1
    println(i)
end

输出:
字典-输出-05
示例2:逐个访问每个键

# Julia program to illustrate 
# the use of Dictionary
  
# Creating a Dictionary with mixed-typed keys
Dict1 = Dict("a" => 1, "b" => 2, "c" => "Hello", 4 => 10)
  
# Printing key-value pair by
# accessing each key one-by-one
for i in keys(Dict1)
    println(i, " => ", Dict1[i])
end

输出:
字典-输出-06
示例 3:通过使用 (key, value) 元组

# Julia program to illustrate 
# the use of Dictionary
  
# Creating a Dictionary with mixed-typed keys
Dict1 = Dict("a" => 1, "b" => 2, "c" => "Hello", 4 => 10)
  
# Printing key-value pair by
# using key and value tuples
for (i, j) in Dict1
    println(i, " => ", j)
end

输出:
字典-输出-07

修改字典的元素

字典元素的修改包括添加新键、修改现有键值和删除键的过程。元素的修改不包括重命名字典的键,但是,可以通过删除现有键并添加另一个具有相同值的键来完成。

# Julia program to illustrate 
# the use of Dictionary
  
# Creating a Dictionary with mixed-typed keys
Dict1 = Dict("a" => 1, "b" => 2, "c" => "Hello", 4 => 10)
println("Initial Dictionary: \n", Dict1)
  
# Adding a new key
Dict1["d"] = 20
println("\nUpdated Dictionary after Adding new key: \n", Dict1)
  
# Updating existing key
Dict1["c"] = "Hello Geeks"
println("\nUpdated Dictionary after Updating a key: \n", Dict1)
  
# Deleting an existing key
Dict1 = delete !(Dict1, "d")
println("\nUpdated Dictionary after Deleting a key: \n", Dict1)

输出:
字典-输出-08

字典方法

Methods Description
get() Used to return the value stored for the specified key, or the given default value if no mapping for the key is present.
get!() Used to return the value stored for the specified key, or if no mapping for the key is present, store key => default, and return default.
getkey() Used to return the key matching argument key if one exists in collection, otherwise return default.
keytype() Used to return the key type of the specified array.
merge() Used to construct a merged collection from the specified collections
merge!() Used to update collection with pairs from the other collections.
pairs() Returns an iterator over key=>value pairs for the specified collection that maps a set of keys to a set of values.
valtype() Used to return the value type of the specified array.