{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Python для сбора и анализа данных\n", "\n", "*Алла Тамбовцева, НИУ ВШЭ*" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Генераторы списков (списковые включения)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "nums = [1, 8, 23, 45, 67]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 64, 529, 2025, 4489]\n" ] } ], "source": [ "nums_sq = [] \n", "for n in nums:\n", " nums_sq.append(n ** 2)\n", "print(nums_sq) " ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 64, 529, 2025, 4489]\n" ] } ], "source": [ "nums_sq = [n ** 2 for n in nums] \n", "print(nums_sq) " ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.52 ms ± 15.9 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" ] } ], "source": [ "%%timeit\n", "R = []\n", "for i in range(0, 5001):\n", " R.append(i ** 3) " ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.35 ms ± 10.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" ] } ], "source": [ "%%timeit\n", "R = [i ** 3 for i in range(0, 5001)] " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Функция `enumerate()`" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "drinks = ['tea', 'coffee', 'juice', 'milk'] " ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 tea\n", "1 coffee\n", "2 juice\n", "3 milk\n" ] } ], "source": [ "for d in drinks:\n", " print(drinks.index(d), d) # метод index()" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 tea\n", "1 coffee\n", "2 juice\n", "3 milk\n" ] } ], "source": [ "for i in range(0, len(drinks)):\n", " print(i, drinks[i]) " ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "enumerate(drinks)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 'tea'), (1, 'coffee'), (2, 'juice'), (3, 'milk')]" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(enumerate(drinks)) # list of tuple" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 tea\n", "1 coffee\n", "2 juice\n", "3 milk\n" ] } ], "source": [ "for i, d in enumerate(drinks):\n", " print(i, d) " ] } ], "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.7.4" } }, "nbformat": 4, "nbformat_minor": 2 }