{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Keep Track while Looping\n" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:34:49.358941Z", "start_time": "2020-07-31T16:34:49.355263Z" } }, "outputs": [], "source": [ "friends = ['Ben', 'Kate', 'Thinh']" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:34:56.448203Z", "start_time": "2020-07-31T16:34:56.445087Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0: Ben\n", "1: Kate\n", "2: Thinh\n" ] } ], "source": [ "# Enumerate\n", "for i, item in enumerate(friends):\n", " print(f'{i}: {item}')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:35:33.841276Z", "start_time": "2020-07-31T16:35:33.833497Z" } }, "outputs": [ { "data": { "text/plain": [ "{0: 'Ben', 1: 'Kate', 2: 'Thinh'}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Dictionary comprehension\n", "{i: friends[i] for i in range(len(friends))}\n", "{0: 'Ben', 1: 'Kate', 2: 'Thinh'}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Update New Dictionary Items" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:36:29.457784Z", "start_time": "2020-07-31T16:36:29.451343Z" } }, "outputs": [ { "data": { "text/plain": [ "Counter({'love': 3, 'hate': 3, 'flower': 1})" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from collections import Counter\n", "\n", "bag_words = Counter()\n", "\n", "sent1 = {'love': 1, 'hate': 3}\n", "bag_words.update(sent1)\n", "\n", "sent2 = {'love': 2, 'flower': 1}\n", "bag_words.update(sent2)\n", "\n", "bag_words" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:36:38.942788Z", "start_time": "2020-07-31T16:36:38.939384Z" } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(bag_words)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:36:43.360612Z", "start_time": "2020-07-31T16:36:43.357196Z" } }, "outputs": [ { "data": { "text/plain": [ "7" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(bag_words.values())" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Define Reusable Object with Namedtuple" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:37:40.865964Z", "start_time": "2020-07-31T16:37:40.860054Z" } }, "outputs": [], "source": [ "from collections import namedtuple\n", "\n", "Friend = namedtuple('Friend' , 'birthday food color introvert')\n", "\n", "Kate = Friend('Feb', 'cake', 'pink', True)\n", "Ben = Friend('Jan', 'fish', 'red', False)\n" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:37:58.026210Z", "start_time": "2020-07-31T16:37:58.022770Z" } }, "outputs": [ { "data": { "text/plain": [ "Friend(birthday='Feb', food='cake', color='pink', introvert=True)" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Kate" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2020-07-31T16:37:59.797947Z", "start_time": "2020-07-31T16:37:59.791973Z" } }, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Ben.introvert" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.7.6 64-bit ('venv': venv)", "language": "python", "name": "python37664bitvenvvenvd0fb19b95beb42129141875f755ed4a0" }, "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.7.6" }, "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 }