Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
khuyentran1401 authored Aug 3, 2020
1 parent e600813 commit 2cfaddf
Show file tree
Hide file tree
Showing 8 changed files with 3,740 additions and 0 deletions.
1,341 changes: 1,341 additions & 0 deletions python/Numpy_tricks.ipynb

Large diffs are not rendered by default.

292 changes: 292 additions & 0 deletions python/Time.ipynb

Large diffs are not rendered by default.

308 changes: 308 additions & 0 deletions python/dictionary.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,308 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Ordered Dictionary"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2020-07-31T16:39:49.565359Z",
"start_time": "2020-07-31T16:39:49.557360Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"OrderedDict([('laundry', 0.5), ('shopping', 2), ('clean', 2), ('movie', 2)])"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import collections\n",
"\n",
"tasks = collections.OrderedDict(laundry=0.5, shopping=2, clean=2)\n",
"tasks['movie'] = 2\n",
"tasks"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2020-07-31T16:40:23.102316Z",
"start_time": "2020-07-31T16:40:23.099361Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"odict_keys(['laundry', 'shopping', 'clean', 'movie'])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tasks.keys()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Default Dictionary"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2020-07-31T16:41:11.055182Z",
"start_time": "2020-07-31T16:41:11.043800Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'B23'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from collections import defaultdict\n",
"\n",
"classes = defaultdict(lambda: 'Outside')\n",
"classes['Math'] = 'B23'\n",
"classes['Physics'] = 'D24'\n",
"classes['Math']"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"ExecuteTime": {
"end_time": "2020-07-31T16:41:19.770211Z",
"start_time": "2020-07-31T16:41:19.763595Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'Outside'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"classes['English']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Group Dictionaries"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2020-07-31T16:42:03.832202Z",
"start_time": "2020-07-31T16:42:03.826726Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"ChainMap({'name': 'Ben', 'age': 23}, {'name': 'Thinh', 'age': 25})"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from collections import ChainMap\n",
"\n",
"#Create multiple dictionaries of friends and their information\n",
"friend1 = {'name':'Ben','age':23}\n",
"friend2 = {'name':'Thinh', 'age': 25}\n",
"\n",
"#Group these dictionaries together\n",
"friends = ChainMap(friend1, friend2)\n",
"friends"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2020-07-31T16:42:19.594914Z",
"start_time": "2020-07-31T16:42:19.591839Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'Ben'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"friends['name']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Read-Only Dictionary "
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"ExecuteTime": {
"end_time": "2020-07-31T16:44:31.196155Z",
"start_time": "2020-07-31T16:44:31.193406Z"
}
},
"outputs": [],
"source": [
"from types import MappingProxyType\n",
"\n",
"#Can read and edit\n",
"classes = {'Math': 'B23','Physics':'D24'}\n",
"classes['Math']\n",
"classes[\"Math\"] = 'B21'\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"ExecuteTime": {
"end_time": "2020-07-31T16:44:32.346687Z",
"start_time": "2020-07-31T16:44:32.341659Z"
}
},
"outputs": [
{
"ename": "TypeError",
"evalue": "'mappingproxy' object does not support item assignment",
"output_type": "error",
"traceback": [
"\u001b[0;31m-----------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-20-72d13339503c>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0mclasses\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'Math'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0;31m#But can no longer edit\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 6\u001b[0;31m \u001b[0mclasses\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m\"Math\"\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m\"D15\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m: 'mappingproxy' object does not support item assignment"
]
}
],
"source": [
"classes = MappingProxyType({'Math': 'B23','Physics':'D24'})\n",
"\n",
"#Can read\n",
"classes['Math']\n",
"#But can no longer edit\n",
"classes[\"Math\"] = \"D15\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
},
"latex_envs": {
"LaTeX_envs_menu_present": true,
"autoclose": false,
"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": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit 2cfaddf

Please sign in to comment.