{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Map Filter" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "l = ['1','2','3','4','5','6']" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(map(int,l))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 16, 25, 36]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(map(lambda x:x**2,map(int,l)))" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5, 6]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "def sq(x):\n", " return x**2\n", "\n", "def cube(x):\n", " return x**3\n", "\n", "func = [sq, cube]\n", "l1 = list(map(int,l))\n", "\n", "l1" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 1]\n", "[4, 8]\n", "[9, 27]\n", "[16, 64]\n", "[25, 125]\n", "[36, 216]\n" ] } ], "source": [ "for i in l1:\n", " val = list(map(lambda x:x(i),func))\n", " print(val)\n", " \n", " " ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[{'x': 1, 'y': 2}, {'x': 1, 'y': 4}]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d = [{\n", " \"x\" : 1,\n", " \"y\" : 2\n", "},{\n", " \"x\" : 1,\n", " \"y\" : 4\n", "},{\n", " \"x\" : 2,\n", " \"y\" : 1\n", "}]\n", "\n", "def filter_x_1(a):\n", " return a[\"x\"] == 1\n", "\n", "list(filter(filter_x_1,d))" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "25" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from functools import reduce \n", "\n", "\n", "a = [1,2,4,5,6,7]\n", "\n", "mul = reduce(lambda x,y: x+y, a)\n", "mul" ] }, { "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" } }, "nbformat": 4, "nbformat_minor": 4 }