{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## CS 200: List Comprehensions\n", "\n", "\n", "\n", "\n", "### Video:\n", "\n", "See List Comprehensions from Socratica.\n", "\n", "List comprehensions are consise ways of executing functions on lists (and sets and dicts). They are inspired by mathematical notation and the Haskell programming language.\n", "\n", "In math, the common ways to describe lists (or sets, or tuples, or vectors) are: \n", "\n", "
  • S = {x² : x in {0 ... 9}} \n", "
  • \n", "
  • V = (1, 2, 4, 8, ..., 2¹²) \n", "
  • \n", "
  • M = {x | x in S and x even} \n", "
  • \n", "\n", " \n", "In other words, you'll find that the above definitions tell you the following:\n", "\n", "> S is a sequence that contains values between 0 and 9 included, and each value is raised to the power of two.\n", " \n", "> The sequence V, on the other hand, contains the value 2 that is raised to a certain power x. The power x starts from 0 and goes till 12.\n", "\n", "> Lastly, the sequence M contains only the even elements from the sequence S.\n", "\n", "See https://www.datacamp.com/community/tutorials/python-list-comprehension\n", " \n", "Below we give examples of equivalent computations using iteration, map, and list comprehensions.\n", "\n", "### Convert a string of digits into a list of digits." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "a = []\n", "for n in str(12345):\n", " a.append(n)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['1', '2', '3', '4', '5']" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "a1 = list(map(lambda x: x, \"12345\"))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['1', '2', '3', '4', '5']" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a1" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "a2 = [x for x in str(12345)]" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['1', '2', '3', '4', '5']" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a2" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a == a1 == a2" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(map(lambda x: x*x, range(10)))" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[-3, -2, 1, 6, 13, 22, 33, 46, 61, 78]" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(map(lambda x:x-3,map(lambda x: x*x,range(10))))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Convert a string of digits into a list of integers" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "b = []\n", "for n in str(12345):\n", " b.append(int(n))" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "b1 = list(map(int,\"12345\"))" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b1" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [], "source": [ "b2 = [int(x) for x in str(12345)]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 5]" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b2" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b == b1 == b2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Convert a string of digits into a list of squares" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [], "source": [ "c = []\n", "for n in str(12345):\n", " c.append(int(n) * int(n))" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 16, 25]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [], "source": [ "c1 = list(map(lambda x: int(x) * int(x), \"12345\"))" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 16, 25]" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c1" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "c2 = [int(x) * int(x) for x in str(12345)]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 4, 9, 16, 25]" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c2" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c == c1 == c2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Convert a range of integers into a list of pairs (tuples) of integers and squares" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "d = []\n", "for n in range(5):\n", " d.append((n, n*n))" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)]" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "d1 = list(map(lambda x: (int(x), int(x) * int(x)), range(5)))" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)]" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d1" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [], "source": [ "d1a = list(map(lambda x: (x, x*x), range(5))) ## do not need the int()" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d1a" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "d2 = [(n, n*n) for n in range(5)]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 0), (1, 1), (2, 4), (3, 9), (4, 16)]" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d2" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d == d1 == d1a == d2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "

    Convert a list of characters into a list of pairs (tuples) of indices and characters

    \n", "\n", "(No map version.)" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [], "source": [ "ee = enumerate([5,4,3,2,1])\n", "ee2 = list(ee)" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ee" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 5), (1, 4), (2, 3), (3, 2), (4, 1)]" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ee2" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "e = []\n", "for (x1,x2) in enumerate(\"abcde\"):\n", " e.append((x1,x2))" ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "e2 = [(x1,x2) for (x1,x2) in enumerate(\"abcde\")]" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e2" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "e == e2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Filter a range of integers, selecting only the even ones\n", "\n", "Uses filter instead of map\n", "\n", "Uses if inside list comprehension." ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "f = []\n", "for x in range(10):\n", " if x % 2 == 0:\n", " f.append(x)" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 2, 4, 6, 8]" ] }, "execution_count": 42, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "f1 = list(filter (lambda x: x % 2 == 0, range(10)))" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 2, 4, 6, 8]" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f1" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "f2 = [x for x in range(10) if x % 2 == 0]" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 2, 4, 6, 8]" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f2" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "f == f1 == f2" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Filters range of integers, selecting the ones greater than 4" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "g = []\n", "for x in range(10):\n", " if x > 4:\n", " g.append(x)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 6, 7, 8, 9]" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [], "source": [ "g1 = list(filter (lambda x: x > 4, range(10)))" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 6, 7, 8, 9]" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g1" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "g2 = [x for x in range(10) if x > 4]" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[5, 6, 7, 8, 9]" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g2" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [], "source": [ "l = ['morse', 'stiles', '', 'franklin', 'edwards','', \"\", '', 'branford', '', 'trumbull', '', '', 'hopper']" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['morse',\n", " 'stiles',\n", " '',\n", " 'franklin',\n", " 'edwards',\n", " '',\n", " '',\n", " '',\n", " 'branford',\n", " '',\n", " 'trumbull',\n", " '',\n", " '',\n", " 'hopper']" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "l" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "14" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(l)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['morse', 'stiles', 'franklin', 'edwards', 'branford', 'trumbull', 'hopper']" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list(filter(None, l))" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['morse', 'stiles', 'franklin', 'edwards', 'branford', 'trumbull', 'hopper']" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x for x in l if x]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### reduce combines values in a list" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [], "source": [ "h = 0\n", "for x in range(101):\n", " h += x" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5050" ] }, "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [], "source": [ "from functools import reduce" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [], "source": [ "h1 = reduce(lambda x, y: x+y, range(101))" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5050" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h1" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "h == h1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Generating prime numbers\n", "\n", "See https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "noprimes = [j for i in range(2, 8) for j in range(i*2, 100, i)]" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[4,\n", " 6,\n", " 8,\n", " 10,\n", " 12,\n", " 14,\n", " 16,\n", " 18,\n", " 20,\n", " 22,\n", " 24,\n", " 26,\n", " 28,\n", " 30,\n", " 32,\n", " 34,\n", " 36,\n", " 38,\n", " 40,\n", " 42,\n", " 44,\n", " 46,\n", " 48,\n", " 50,\n", " 52,\n", " 54,\n", " 56,\n", " 58,\n", " 60,\n", " 62,\n", " 64,\n", " 66,\n", " 68,\n", " 70,\n", " 72,\n", " 74,\n", " 76,\n", " 78,\n", " 80,\n", " 82,\n", " 84,\n", " 86,\n", " 88,\n", " 90,\n", " 92,\n", " 94,\n", " 96,\n", " 98,\n", " 6,\n", " 9,\n", " 12,\n", " 15,\n", " 18,\n", " 21,\n", " 24,\n", " 27,\n", " 30,\n", " 33,\n", " 36,\n", " 39,\n", " 42,\n", " 45,\n", " 48,\n", " 51,\n", " 54,\n", " 57,\n", " 60,\n", " 63,\n", " 66,\n", " 69,\n", " 72,\n", " 75,\n", " 78,\n", " 81,\n", " 84,\n", " 87,\n", " 90,\n", " 93,\n", " 96,\n", " 99,\n", " 8,\n", " 12,\n", " 16,\n", " 20,\n", " 24,\n", " 28,\n", " 32,\n", " 36,\n", " 40,\n", " 44,\n", " 48,\n", " 52,\n", " 56,\n", " 60,\n", " 64,\n", " 68,\n", " 72,\n", " 76,\n", " 80,\n", " 84,\n", " 88,\n", " 92,\n", " 96,\n", " 10,\n", " 15,\n", " 20,\n", " 25,\n", " 30,\n", " 35,\n", " 40,\n", " 45,\n", " 50,\n", " 55,\n", " 60,\n", " 65,\n", " 70,\n", " 75,\n", " 80,\n", " 85,\n", " 90,\n", " 95,\n", " 12,\n", " 18,\n", " 24,\n", " 30,\n", " 36,\n", " 42,\n", " 48,\n", " 54,\n", " 60,\n", " 66,\n", " 72,\n", " 78,\n", " 84,\n", " 90,\n", " 96,\n", " 14,\n", " 21,\n", " 28,\n", " 35,\n", " 42,\n", " 49,\n", " 56,\n", " 63,\n", " 70,\n", " 77,\n", " 84,\n", " 91,\n", " 98]" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "noprimes" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "primes = [x for x in range(2, 100) if x not in noprimes]" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[2,\n", " 3,\n", " 5,\n", " 7,\n", " 11,\n", " 13,\n", " 17,\n", " 19,\n", " 23,\n", " 29,\n", " 31,\n", " 37,\n", " 41,\n", " 43,\n", " 47,\n", " 53,\n", " 59,\n", " 61,\n", " 67,\n", " 71,\n", " 73,\n", " 79,\n", " 83,\n", " 89,\n", " 97]" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "primes" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "149" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(noprimes)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "noprimes contains duplicates. If we convert it to a set, we will eliminate the duplicates." ] }, { "cell_type": "code", "execution_count": 70, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5, 6, 7}" ] }, "execution_count": 70, "metadata": {}, "output_type": "execute_result" } ], "source": [ "set([1,2,3,4,1,2,3,4,5,6,7])" ] }, { "cell_type": "code", "execution_count": 71, "metadata": {}, "outputs": [], "source": [ "setnoprimes = set(noprimes)" ] }, { "cell_type": "code", "execution_count": 72, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{4,\n", " 6,\n", " 8,\n", " 9,\n", " 10,\n", " 12,\n", " 14,\n", " 15,\n", " 16,\n", " 18,\n", " 20,\n", " 21,\n", " 22,\n", " 24,\n", " 25,\n", " 26,\n", " 27,\n", " 28,\n", " 30,\n", " 32,\n", " 33,\n", " 34,\n", " 35,\n", " 36,\n", " 38,\n", " 39,\n", " 40,\n", " 42,\n", " 44,\n", " 45,\n", " 46,\n", " 48,\n", " 49,\n", " 50,\n", " 51,\n", " 52,\n", " 54,\n", " 55,\n", " 56,\n", " 57,\n", " 58,\n", " 60,\n", " 62,\n", " 63,\n", " 64,\n", " 65,\n", " 66,\n", " 68,\n", " 69,\n", " 70,\n", " 72,\n", " 74,\n", " 75,\n", " 76,\n", " 77,\n", " 78,\n", " 80,\n", " 81,\n", " 82,\n", " 84,\n", " 85,\n", " 86,\n", " 87,\n", " 88,\n", " 90,\n", " 91,\n", " 92,\n", " 93,\n", " 94,\n", " 95,\n", " 96,\n", " 98,\n", " 99}" ] }, "execution_count": 72, "metadata": {}, "output_type": "execute_result" } ], "source": [ "setnoprimes" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "73" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(setnoprimes)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Set comprehensions\n", "\n", "By using {} instead of [] we can create set comprehensions." ] }, { "cell_type": "code", "execution_count": 74, "metadata": {}, "outputs": [], "source": [ "s1 = [x if x % 2 else x + 1 for x in range(10)]" ] }, { "cell_type": "code", "execution_count": 75, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 1, 3, 3, 5, 5, 7, 7, 9, 9]" ] }, "execution_count": 75, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [], "source": [ "s2 = {x if x % 2 else x + 1 for x in range(10)}" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 3, 5, 7, 9}" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can create literal sets using {}" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [], "source": [ "s3 = {1, 2, 3, 1, 2, 3}" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3}" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s3" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [], "source": [ "noprimesset = {j for i in range(2, 8) for j in range(i*2, 100, i)}" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{4,\n", " 6,\n", " 8,\n", " 9,\n", " 10,\n", " 12,\n", " 14,\n", " 15,\n", " 16,\n", " 18,\n", " 20,\n", " 21,\n", " 22,\n", " 24,\n", " 25,\n", " 26,\n", " 27,\n", " 28,\n", " 30,\n", " 32,\n", " 33,\n", " 34,\n", " 35,\n", " 36,\n", " 38,\n", " 39,\n", " 40,\n", " 42,\n", " 44,\n", " 45,\n", " 46,\n", " 48,\n", " 49,\n", " 50,\n", " 51,\n", " 52,\n", " 54,\n", " 55,\n", " 56,\n", " 57,\n", " 58,\n", " 60,\n", " 62,\n", " 63,\n", " 64,\n", " 65,\n", " 66,\n", " 68,\n", " 69,\n", " 70,\n", " 72,\n", " 74,\n", " 75,\n", " 76,\n", " 77,\n", " 78,\n", " 80,\n", " 81,\n", " 82,\n", " 84,\n", " 85,\n", " 86,\n", " 87,\n", " 88,\n", " 90,\n", " 91,\n", " 92,\n", " 93,\n", " 94,\n", " 95,\n", " 96,\n", " 98,\n", " 99}" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "noprimesset" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "73" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(noprimesset)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Dictionary comprehensions" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "d = {i : x*x for (i,x) in enumerate(range(10))}" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81}" ] }, "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "64" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d[8]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "End of list comprehension notebook" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.7" } }, "nbformat": 4, "nbformat_minor": 4 }