在 Julia 中获取字典的键值对——pairs() 方法
pairs()
是 julia 中的一个内置函数,用于为指定集合返回key=>value对的迭代器,该集合将一组键映射到一组值。这包括数组,其中键是数组索引。
Syntax:
pairs(collection)
or
pairs(IndexStyle(A), A)
Parameters:
- collection: Specified collection.
- IndexStyle(A): Specified iterator.
- A: Specified array.
Returns: It returns an iterator over key=>value pairs for the specified collection that maps a set of keys to a set of values.
示例 1:
# Julia program to illustrate
# the use of pairs() method
# Getting an iterator over key =>value
# pairs for the specified collection
# that maps a set of keys to a set of values.
A = ["a" "c"; "b" "d"];
println(pairs(A))
输出:
示例 2:
# Julia program to illustrate
# the use of pairs() method
# Getting an iterator over key =>value
# pairs for the specified array
A = ["a" "c"; "b" "d"];
for (index, value) in pairs(IndexStyle(A), A)
println("$index $value")
end
输出: