|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "[<img src=\"http://cloud.blobcity.net/assets/images/badge.png\" height=\"20\" style=\"margin-bottom:-15px\" />](https://cloud.blobcity.com/#/ps/shared-cloudbook/66c4fcaa-b0e4-4e0a-b275-49cdf007667a)\n", |
| 8 | + "\n", |
| 9 | + "# Sequence Functions\n", |
| 10 | + "\n", |
| 11 | + "Python offers several sequence functions. These are useful and commonly used for several data operations. We will take a look at few of these that are most commonly used.\n", |
| 12 | + "\n", |
| 13 | + "## Enumerate\n", |
| 14 | + "We often require to interate over collection classes and also have the current index of the interaction. We usually have to do this by initialising a new variable, commonly called `i`, and then keeping track of it by manually incrementing it with every logical iteration. \n", |
| 15 | + "\n", |
| 16 | + "This requirement is so common, that almost every developer defines and manages this `i`; and possibly does so several times everyday. \n", |
| 17 | + "\n", |
| 18 | + "Python has created a built-in enumerate function, that creates tuples of `i` and the corresponding sequence value. This help create an automatic track of the iteration index `i`. Here is an example." |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": 1, |
| 24 | + "metadata": {}, |
| 25 | + "outputs": [ |
| 26 | + { |
| 27 | + "name": "stdout", |
| 28 | + "output_type": "stream", |
| 29 | + "text": [ |
| 30 | + "0 -> A\n", |
| 31 | + "1 -> B\n", |
| 32 | + "2 -> C\n", |
| 33 | + "3 -> D\n", |
| 34 | + "4 -> E\n" |
| 35 | + ] |
| 36 | + } |
| 37 | + ], |
| 38 | + "source": [ |
| 39 | + "my_list = ['A', 'B', 'C', 'D', 'E']\n", |
| 40 | + "for i, item in enumerate(my_list):\n", |
| 41 | + " print(i, '->', item)" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "markdown", |
| 46 | + "metadata": {}, |
| 47 | + "source": [ |
| 48 | + "As we can see, the `enumerate` function returns a combination of `i` and `item`. This combination is actually a Tuple of the form `(i, item)`\n", |
| 49 | + "\n", |
| 50 | + "## Sorted\n", |
| 51 | + "\n", |
| 52 | + "Python provides a built-in function to sort elements of any sequence. If a list is provided, the contents of the list are sorted. However the sorted function can be used to sort strings as well. Here is an example" |
| 53 | + ] |
| 54 | + }, |
| 55 | + { |
| 56 | + "cell_type": "code", |
| 57 | + "execution_count": 2, |
| 58 | + "metadata": {}, |
| 59 | + "outputs": [ |
| 60 | + { |
| 61 | + "data": { |
| 62 | + "text/plain": [ |
| 63 | + "[0, 2, 4, 5, 6, 8, 9]" |
| 64 | + ] |
| 65 | + }, |
| 66 | + "execution_count": 2, |
| 67 | + "metadata": {}, |
| 68 | + "output_type": "execute_result" |
| 69 | + } |
| 70 | + ], |
| 71 | + "source": [ |
| 72 | + "sorted([6,2,8,0,4,5,9])" |
| 73 | + ] |
| 74 | + }, |
| 75 | + { |
| 76 | + "cell_type": "markdown", |
| 77 | + "metadata": {}, |
| 78 | + "source": [ |
| 79 | + "Let's look at an example use of sorted with a string" |
| 80 | + ] |
| 81 | + }, |
| 82 | + { |
| 83 | + "cell_type": "code", |
| 84 | + "execution_count": 3, |
| 85 | + "metadata": {}, |
| 86 | + "outputs": [ |
| 87 | + { |
| 88 | + "name": "stdout", |
| 89 | + "output_type": "stream", |
| 90 | + "text": [ |
| 91 | + "[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'a', 'b', 'c', 'd', 'e', 'e', 'e', 'f', 'g', 'h', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'o', 'o', 'o', 'p', 'q', 'r', 'r', 's', 't', 't', 'u', 'u', 'v', 'w', 'x', 'y', 'z']\n" |
| 92 | + ] |
| 93 | + } |
| 94 | + ], |
| 95 | + "source": [ |
| 96 | + "print(sorted('the quick brown fox jumps over the lazy dog'))" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "markdown", |
| 101 | + "metadata": {}, |
| 102 | + "source": [ |
| 103 | + "We can see that the `sorted` function performed a sort on all characters of the string. The spaces get aligned first, followed by all the individual alphabets in a sorted manner. \n", |
| 104 | + "\n", |
| 105 | + "The `print` is used for better formatting of the output array. However, the sort logic does work without the `print` and is completely unreleated to the usage of the `print` function.\n", |
| 106 | + "\n", |
| 107 | + "## Zip\n", |
| 108 | + "\n", |
| 109 | + "The zip statement is used to combine two or more sequences to form a combined sequence of tuples. Let's look at an example to understand this best. " |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "code", |
| 114 | + "execution_count": 4, |
| 115 | + "metadata": {}, |
| 116 | + "outputs": [ |
| 117 | + { |
| 118 | + "name": "stdout", |
| 119 | + "output_type": "stream", |
| 120 | + "text": [ |
| 121 | + "[('Lion', 'Animal'), ('Apple', 'Fruit'), ('Rose', 'Flower')]\n" |
| 122 | + ] |
| 123 | + } |
| 124 | + ], |
| 125 | + "source": [ |
| 126 | + "items = ['Lion', 'Apple', 'Rose']\n", |
| 127 | + "categories = ['Animal', 'Fruit', 'Flower']\n", |
| 128 | + "\n", |
| 129 | + "zipped = zip(items, categories)\n", |
| 130 | + "\n", |
| 131 | + "print(list(zipped))" |
| 132 | + ] |
| 133 | + }, |
| 134 | + { |
| 135 | + "cell_type": "markdown", |
| 136 | + "metadata": {}, |
| 137 | + "source": [ |
| 138 | + "Interestingly, the `zip` function combined the two lists to form a collection of tuples. `('Lion', 'Animal')` is of type Tuple. But what is the type of the variable `zipped`. Let's find out. " |
| 139 | + ] |
| 140 | + }, |
| 141 | + { |
| 142 | + "cell_type": "code", |
| 143 | + "execution_count": 5, |
| 144 | + "metadata": {}, |
| 145 | + "outputs": [], |
| 146 | + "source": [ |
| 147 | + "?zipped" |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "markdown", |
| 152 | + "metadata": {}, |
| 153 | + "source": [ |
| 154 | + "The variable `zipped` is of type `zip` itself. `zip` is an in-built implementation available in Python. A `zip` type can be converted into a `list` as shown in the above example. \n", |
| 155 | + "\n", |
| 156 | + "The `zip` operation works to combine more than one sequence as well." |
| 157 | + ] |
| 158 | + }, |
| 159 | + { |
| 160 | + "cell_type": "code", |
| 161 | + "execution_count": 6, |
| 162 | + "metadata": {}, |
| 163 | + "outputs": [ |
| 164 | + { |
| 165 | + "name": "stdout", |
| 166 | + "output_type": "stream", |
| 167 | + "text": [ |
| 168 | + "[('Lion', 'Animal', 'Dangerous'), ('Apple', 'Fruit', 'Healthy'), ('Rose', 'Flower', 'Red')]\n" |
| 169 | + ] |
| 170 | + } |
| 171 | + ], |
| 172 | + "source": [ |
| 173 | + "items = ['Lion', 'Apple', 'Rose']\n", |
| 174 | + "categories = ['Animal', 'Fruit', 'Flower']\n", |
| 175 | + "characteristics = ['Dangerous', 'Healthy', 'Red']\n", |
| 176 | + "\n", |
| 177 | + "zipped = zip(items, categories, characteristics)\n", |
| 178 | + "\n", |
| 179 | + "print(list(zipped))" |
| 180 | + ] |
| 181 | + }, |
| 182 | + { |
| 183 | + "cell_type": "markdown", |
| 184 | + "metadata": {}, |
| 185 | + "source": [ |
| 186 | + "A `zip` can also be used with `enumerate` to iterate over the tuples inside the zip." |
| 187 | + ] |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "code", |
| 191 | + "execution_count": 7, |
| 192 | + "metadata": {}, |
| 193 | + "outputs": [ |
| 194 | + { |
| 195 | + "name": "stdout", |
| 196 | + "output_type": "stream", |
| 197 | + "text": [ |
| 198 | + "Steve Jobs\n", |
| 199 | + "Bill Gates\n", |
| 200 | + "Larry Page\n" |
| 201 | + ] |
| 202 | + } |
| 203 | + ], |
| 204 | + "source": [ |
| 205 | + "first_name = ['Steve', 'Bill', 'Larry']\n", |
| 206 | + "last_name = ['Jobs', 'Gates', 'Page']\n", |
| 207 | + "\n", |
| 208 | + "full_name = zip(first_name, last_name)\n", |
| 209 | + "\n", |
| 210 | + "for i, (fn, ln) in enumerate(full_name): \n", |
| 211 | + " print(fn, ln)" |
| 212 | + ] |
| 213 | + }, |
| 214 | + { |
| 215 | + "cell_type": "markdown", |
| 216 | + "metadata": {}, |
| 217 | + "source": [ |
| 218 | + "The above example also shows a more practical application of the `zip` function. It is very common that in the data you have, first name and last names of individuals are stored as independent columns in a database. When you pick up such data from the database, you might find the `zip` function very useful to creates tuples of individual rows of the data. \n", |
| 219 | + "\n", |
| 220 | + "## Reversed\n", |
| 221 | + "\n", |
| 222 | + "The reversed function is a very commonly used function. It's basic use is to provide the contents of a sequence in the reverse order. " |
| 223 | + ] |
| 224 | + }, |
| 225 | + { |
| 226 | + "cell_type": "code", |
| 227 | + "execution_count": 8, |
| 228 | + "metadata": {}, |
| 229 | + "outputs": [ |
| 230 | + { |
| 231 | + "name": "stdout", |
| 232 | + "output_type": "stream", |
| 233 | + "text": [ |
| 234 | + "Original [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n", |
| 235 | + "Reversed [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]\n" |
| 236 | + ] |
| 237 | + } |
| 238 | + ], |
| 239 | + "source": [ |
| 240 | + "my_range = list(range(10))\n", |
| 241 | + "reversed_range = list(reversed(my_range))\n", |
| 242 | + "\n", |
| 243 | + "print('Original', my_range)\n", |
| 244 | + "print('Reversed', reversed_range)" |
| 245 | + ] |
| 246 | + }, |
| 247 | + { |
| 248 | + "cell_type": "markdown", |
| 249 | + "metadata": {}, |
| 250 | + "source": [ |
| 251 | + "The use of the `reversed` function does not affect the contents of the original sequence. Thereby the contents are not actually reversed, but the seuquence is simply iterated in a reverse order." |
| 252 | + ] |
| 253 | + }, |
| 254 | + { |
| 255 | + "cell_type": "markdown", |
| 256 | + "metadata": {}, |
| 257 | + "source": [ |
| 258 | + "***\n", |
| 259 | + "Copyright (c) 2020, BlobCity, Inc." |
| 260 | + ] |
| 261 | + } |
| 262 | + ], |
| 263 | + "metadata": { |
| 264 | + "kernelspec": { |
| 265 | + "display_name": "Python 3", |
| 266 | + "language": "python", |
| 267 | + "name": "python3" |
| 268 | + }, |
| 269 | + "language_info": { |
| 270 | + "codemirror_mode": { |
| 271 | + "name": "ipython", |
| 272 | + "version": 3 |
| 273 | + }, |
| 274 | + "file_extension": ".py", |
| 275 | + "mimetype": "text/x-python", |
| 276 | + "name": "python", |
| 277 | + "nbconvert_exporter": "python", |
| 278 | + "pygments_lexer": "ipython3", |
| 279 | + "version": "3.8.4" |
| 280 | + } |
| 281 | + }, |
| 282 | + "nbformat": 4, |
| 283 | + "nbformat_minor": 4 |
| 284 | +} |
0 commit comments