Skip to content

Instantly share code, notes, and snippets.

@St-Hakky
Created February 1, 2018 00:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save St-Hakky/ef18ea125376a4ee94f547b8e58c0507 to your computer and use it in GitHub Desktop.
Save St-Hakky/ef18ea125376a4ee94f547b8e58c0507 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"toc": "true"
},
"source": [
"# Table of Contents\n",
" <p><div class=\"lev1 toc-item\"><a href=\"#Dictionaries\" data-toc-modified-id=\"Dictionaries-1\"><span class=\"toc-item-num\">1&nbsp;&nbsp;</span>Dictionaries</a></div><div class=\"lev1 toc-item\"><a href=\"#辞書型の操作\" data-toc-modified-id=\"辞書型の操作-2\"><span class=\"toc-item-num\">2&nbsp;&nbsp;</span>辞書型の操作</a></div>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Dictionaries"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-01T09:04:40.727000Z",
"start_time": "2018-02-01T00:04:39.399Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Dict{String,Int64} with 3 entries:\n",
" \"Apple\" => 1\n",
" \"Orange\" => 3\n",
" \"Banana\" => 2"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 定義の仕方は以下の通り。Pythonは「:」だが、Juliaでは「=>」になる。\n",
"fruits = Dict(\"Apple\" => 1, \"Banana\" => 2, \"Orange\" => 3)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-01T09:04:40.860000Z",
"start_time": "2018-02-01T00:04:39.998Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 要素へのアクセス方法はキーを指定するだけ\n",
"fruits[\"Apple\"]"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-01T09:04:41.273000Z",
"start_time": "2018-02-01T00:04:40.606Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dict(\"Apple\"=>1,\"Orange\"=>3,\"Peach\"=>4,\"Banana\"=>2)\n",
"3\n",
"Dict(\"Apple\"=>1,\"Orange\"=>3,\"Banana\"=>2)\n"
]
},
{
"data": {
"text/plain": [
"Dict{String,Int64} with 3 entries:\n",
" \"Apple\" => 1\n",
" \"Orange\" => 3\n",
" \"Banana\" => 2"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 要素の追加\n",
"fruits[\"Peach\"] = 4\n",
"println(fruits)\n",
"\n",
"# 更新\n",
"fruits[\"Peach\"] = 3\n",
"println(fruits[\"Peach\"])\n",
"\n",
"# 削除\n",
"pop!(fruits, \"Peach\")\n",
"println(fruits)\n",
"\n",
"# popでもできるが、delete!を使う方が直感的\n",
"fruits[\"Peach\"] = 3\n",
"delete!(fruits, \"Peach\")"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-01T09:04:42.154000Z",
"start_time": "2018-02-01T00:04:41.573Z"
}
},
"outputs": [
{
"ename": "LoadError",
"evalue": "\u001b[91mKeyError: key 1 not found\u001b[39m",
"output_type": "error",
"traceback": [
"\u001b[91mKeyError: key 1 not found\u001b[39m",
"",
"Stacktrace:",
" [1] \u001b[1mgetindex\u001b[22m\u001b[22m\u001b[1m(\u001b[22m\u001b[22m::Dict{String,Int64}, ::Int64\u001b[1m)\u001b[22m\u001b[22m at \u001b[1m.\\dict.jl:474\u001b[22m\u001b[22m"
]
}
],
"source": [
"# 辞書型は順序という概念を持っていないので、indexでアクセスはできない\n",
"# 以下のようにすると、「1」というキーでアクセスしようとする\n",
"fruits[1]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-01T09:04:42.999000Z",
"start_time": "2018-02-01T00:04:42.918Z"
}
},
"outputs": [
{
"ename": "LoadError",
"evalue": "\u001b[91msyntax: { } vector syntax is discontinued\u001b[39m",
"output_type": "error",
"traceback": [
"\u001b[91msyntax: { } vector syntax is discontinued\u001b[39m",
""
]
}
],
"source": [
"# ちなみにPythonみたく定義はできない\n",
"a = {\"Apple\":1}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 辞書型の操作"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-01T09:04:46.613000Z",
"start_time": "2018-02-01T00:04:46.462Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"true"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# キーを持っているかどうかの判定\n",
"haskey(fruits, \"Apple\") # true"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-01T09:04:46.663000Z",
"start_time": "2018-02-01T00:04:46.639Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"100\n"
]
}
],
"source": [
"# get(dict, key, default)で、値を取得できる。defaultは、keyがdictになかったときに返ってくる値\n",
"\n",
"# 以下は、1が返ってくる\n",
"println(get(fruits, \"Apple\", 100))\n",
"\n",
"# Peachがkeyにないので、100を返す\n",
"println(get(fruits, \"Peach\", 100))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-01T09:04:46.829000Z",
"start_time": "2018-02-01T00:04:46.822Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"Dict(\"Apple\"=>1,\"Orange\"=>3,\"Banana\"=>2)\n"
]
}
],
"source": [
"# popを実行する\n",
"fruits[\"Peach\"] = 3\n",
"println(pop!(fruits, \"Peach\")) # Peachの値が返ってくる\n",
"println(fruits) # Peachのkeyとvalueはなくなっている"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-01T09:04:47.571000Z",
"start_time": "2018-02-01T00:04:47.311Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Base.KeyIterator for a Dict{String,Int64} with 3 entries. Keys:\n",
" \"Apple\"\n",
" \"Orange\"\n",
" \"Banana\""
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# keyの一覧を取得したいときは以下のようにしてできる。\n",
"keys(fruits)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-01T09:04:48.079000Z",
"start_time": "2018-02-01T00:04:47.878Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"Base.ValueIterator for a Dict{String,Int64} with 3 entries. Values:\n",
" 1\n",
" 3\n",
" 2"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# valueの一覧を取得したいときは以下のような感じ\n",
"values(fruits)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2018-02-01T09:04:48.760000Z",
"start_time": "2018-02-01T00:04:48.718Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dict(\"Apple\"=>1,\"Peach\"=>4,\"Banana\"=>3)\n",
"Dict(\"Apple\"=>1,\"Peach\"=>4,\"Banana\"=>2)\n"
]
}
],
"source": [
"# 複数の辞書のマージも可能\n",
"a = Dict(\"Apple\" => 1, \"Banana\" => 2)\n",
"b = Dict(\"Banana\" => 3, \"Peach\" => 4)\n",
"\n",
"# 以下の二つのBananaの値の違いに注目\n",
"println(merge(a, b))\n",
"println(merge(b, a))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 0.6.0",
"language": "julia",
"name": "julia-0.6"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "0.6.0"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autocomplete": true,
"bibliofile": "biblio.bib",
"cite_by": "apalike",
"current_citInitial": 1,
"eqLabelWithNumbers": true,
"eqNumInitial": 1,
"hotkeys": {
"equation": "Ctrl-E",
"itemize": "Ctrl-I"
},
"labels_anchors": false,
"latex_user_defs": false,
"report_style_numbering": false,
"user_envs_cfg": false
},
"toc": {
"colors": {
"hover_highlight": "#DAA520",
"navigate_num": "#000000",
"navigate_text": "#333333",
"running_highlight": "#FF0000",
"selected_highlight": "#FFD700",
"sidebar_border": "#EEEEEE",
"wrapper_background": "#FFFFFF"
},
"moveMenuLeft": true,
"nav_menu": {
"height": "39px",
"width": "254px"
},
"navigate_menu": true,
"number_sections": true,
"sideBar": true,
"threshold": 4,
"toc_cell": true,
"toc_section_display": "block",
"toc_window_display": true,
"widenNotebook": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment