當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Julia pairs()用法及代碼示例

這個pairs()是 julia 中的一個內置函數,用於返回指定集合的​​ key=>value 對的迭代器,該集合將一組鍵映射到一組值。這包括數組,其中鍵是數組索引。

用法:
pairs(collection)
or
pairs(IndexStyle(A), A)

參數:

  • collection:指定集合。
  • IndexStyle(A):指定的迭代器。
  • A:指定數組。

返回值:它返回指定集合的​​ key=>value 對上的迭代器,該集合將一組鍵映射到一組值。

範例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

輸出:

相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 Getting key value pairs of a dictionary in Julia – pairs() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。