1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

知っていて当たり前-13 辞書型

Last updated at Posted at 2022-08-07

知っていて当たり前-13 辞書型

1. 空の辞書

dic = Dict()
Dict{Any, Any}()

2. 辞書の定義法

dic = Dict("a" => 1, "foo" => "bar")
Dict{String, Any} with 2 entries:
  "a"   => 1
  "foo" => "bar"

3. キーで値を取り出す

dic["a"] # 名前で取り出す
1
dic[:"foo"] # シンボルで取り出す
"bar"

4. キーが存在するか(定義されているか) haskey()

haskey(dic, "a")
true

5. キーの一覧 keys()

keys(dic)
KeySet for a Dict{String, Any} with 2 entries. Keys:
  "a"
  "foo"

6. 値の一覧 values()

values(dic)
ValueIterator for a Dict{String, Any} with 2 entries. Values:
  1
  "bar"

6.1. 要素の追加

dic["b"] = 999;
dic["foo2"] = "bar2";
dic
Dict{String, Any} with 4 entries:
  "b"    => 999
  "foo2" => "bar2"
  "a"    => 1
  "foo"  => "bar"

7. 要素の削除 delete!()

戻り値は削除された後の辞書

x = delete!(dic, "a")
x
Dict{String, Any} with 3 entries:
  "b"    => 999
  "foo2" => "bar2"
  "foo"  => "bar"
dic # 戻り値と同じ(インプレースで処理された)
Dict{String, Any} with 3 entries:
  "b"    => 999
  "foo2" => "bar2"
  "foo"  => "bar"

8. 要素のポップ(削除) pop!()

戻り値はポップされた値。変数に代入されなければ捨てられるだけ。

x = pop!(dic, "b")
dic
Dict{String, Any} with 2 entries:
  "foo2" => "bar2"
  "foo"  => "bar"
x
999

9. マージ(混ぜ合わせ)

9.1. merge()

merge() の場合,戻り値はマージされた辞書。同じキーがある場合,後者の値が有効になる。

a = Dict("foo" => 0.0, "bar" => 42.0);
b = Dict("baz" => 17, "bar" => 4711);
merge(a, b)
Dict{String, Float64} with 3 entries:
  "bar" => 4711.0
  "baz" => 17.0
  "foo" => 0.0
merge(b, a)
Dict{String, Float64} with 3 entries:
  "bar" => 42.0
  "baz" => 17.0
  "foo" => 0.0

9.2. merge!()

merge!() の場合,戻り値はマージされた第 1 引数の辞書。第 1 引数はインプレースでマージされる。同じキーがある場合,後者の値が有効になる。第 2 引数の辞書は不変。

x = merge!(a, b)
a
Dict{String, Float64} with 3 entries:
  "bar" => 4711.0
  "baz" => 17.0
  "foo" => 0.0
x
Dict{String, Float64} with 3 entries:
  "bar" => 4711.0
  "baz" => 17.0
  "foo" => 0.0
b
Dict{String, Int64} with 2 entries:
  "bar" => 4711
  "baz" => 17

9.3. mergewith()

マージされる辞書に同じキーのものがある場合,値は両方の値を第 1 引数で演算したものになる。

下の例で "bar" は 42.0 + 47.11 の 4753.0 になる。

a = Dict("foo" => 0.0, "bar" => 42.0);
b = Dict("baz" => 17, "bar" => 4711);
x = mergewith(+, a, b)
x
Dict{String, Float64} with 3 entries:
  "bar" => 4753.0
  "baz" => 17.0
  "foo" => 0.0

9.4. mergewith!()

mergewith() と同じであるが,第 2 引数はインプレースで処理される。戻り値は処理後の第 2 引数と同じ。第 3 引数は不変。

a = Dict("foo" => 0.0, "bar" => 42.0);
b = Dict("baz" => 17, "bar" => 4711);
x = mergewith!(+, a, b)
a
Dict{String, Float64} with 3 entries:
  "bar" => 4753.0
  "baz" => 17.0
  "foo" => 0.0
x
Dict{String, Float64} with 3 entries:
  "bar" => 4753.0
  "baz" => 17.0
  "foo" => 0.0
b
Dict{String, Int64} with 2 entries:
  "bar" => 4711
  "baz" => 17

10. 空か? isempty()

isempty(dic)
false

11. 長さ length()

length(dic)
2

12. 空にする empty!()

empty!(dic)
Dict{String, Any}()
isempty(dic)
true
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?