在朱莉娅,如何合并字典?

时间:2016-03-26 20:34:27

标签: dictionary merge julia

在Julia中合并字典的最佳方法是什么?

> dict1 = Dict("a" => 1, "b" => 2, "c" => 3)
> dict2 = Dict("d" => 4, "e" => 5, "f" => 6)
# merge both dicts
> dict3 = dict1 with dict2
> dict3
Dict{ASCIIString,Int64} with 6 entries:
  "f" => 6
  "c" => 3
  "e" => 5
  "b" => 2
  "a" => 1
  "d" => 4

2 个答案:

答案 0 :(得分:13)

https://docs.julialang.org/en/latest/base/collections/#Base.merge

merge(collection, others...)

Construct a merged collection from the given collections. If necessary, the types of the resulting collection will be promoted to accommodate the types of the merged collections. If the same key is present in another collection, the value for that key will be the value it has in the last collection listed.

julia> merge(dict1,dict2)
    Dict{ASCIIString,Int64} with 6 entries:
      "f" => 6
      "c" => 3
      "e" => 5
      "b" => 2
      "a" => 1
      "d" => 4

merge!(collection, others...)
Update collection with pairs from the other collections.

julia> merge!(dict1,dict2)
Dict{ASCIIString,Int64} with 6 entries:
  "f" => 6
  "c" => 3
  "e" => 5
  "b" => 2
  "a" => 1
  "d" => 4

julia> dict1
Dict{ASCIIString,Int64} with 6 entries:
  "f" => 6
  "c" => 3
  "e" => 5
  "b" => 2
  "a" => 1
  "d" => 4

答案 1 :(得分:0)

您可以使用merge。如果Imports System.Runtime.InteropServices Module ManipulateWindows Const SW_HIDE As Integer = 0 Const SW_RESTORE As Integer = 1 Const SW_MINIMIZE As Integer = 2 Const SW_MAXIMIZE As Integer = 3 <DllImport("User32")> _ Private Function ShowWindow(ByVal hwnd As Integer, ByVal nCmdShow As Integer) As Integer End Function Public Sub Main() 'iterate through all the open processes. For Each p As Process In Process.GetProcesses 'Get the Window Handle Dim hWnd as integer = CType(p.MainWindowHandle, Integer) 'Write out the title of the main window for the process. System.Console.WriteLine(p.MainWindowTitle) 'Minimize the Window ShowWindow(hWnd, SW_MINIMIZE) Next p End Sub End Module 的元素具有相同的键,则该键的值将为列出的最后Dict。如果要合并具有相同键的Dict的元素,则可以使用Dictmerge(combine, collection, others...)是一个接收两个值并返回值的函数。

docs的示例:

combine