{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# List comprehension, generator expression\n", "A list comprehension executes immediately and returns a list, which is an iterable object.\n", "\n", "A generator expression is an iterator object. It does almost the same as list comprehension but does it lazily.\n", "\n", "Iterable is an object, which one can iterate over. It generates an Iterator when passed to `iter()` method. Iterator is an object, which is used to iterate over an iterable object using `__next__()` method. Iterators have `__next__()` method, which returns the next item of the object.\n", "\n", "Note that every iterator is also an iterable, but not every iterable is an iterator." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for num in [2, 3, 5, 7]:\n", " print(num, end=' ')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "numbs = [2, 3, 5, 7]\n", "iterator_obj = iter(numbs)\n", "print(next(iterator_obj), end=' ')\n", "print(next(iterator_obj), end=' ')\n", "print(next(iterator_obj), end=' ')\n", "print(next(iterator_obj), end=' ')\n", "print(next(iterator_obj))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "iterator_obj = iter(numbs)\n", "try:\n", " print(next(iterator_obj), end=' ')\n", " print(next(iterator_obj), end=' ')\n", " print(next(iterator_obj), end=' ')\n", " print(next(iterator_obj), end=' ')\n", " print(next(iterator_obj)) #StopIteration error\n", "except:\n", " pass" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's see the differences between list comprehension and generator expression again:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lst = [n for n in range(10000)]\n", "gen = (n for n in range(10000))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(type(lst), type(gen))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from sys import getsizeof\n", "print(getsizeof(lst), getsizeof(gen))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in lst:\n", " if i == 100:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for i in gen:\n", " if i == 100:\n", " print(i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "lst[100]" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "gen[100] # we can iterate through the items" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A 'generator' object is not subscriptable, and we can iterate over it only once. So in embedded loops, we can not use two generator expression:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "g1=(i for i in range(3))\n", "g2=(j for j in range(3))\n", "for i in g1:\n", " for j in g2:\n", " print(i,j)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# we have to generate g2 at every cicle\n", "g1=(i for i in range(3))\n", "for i in g1:\n", " g2=(j for j in range(3))\n", " for j in g2:\n", " print(i,j)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conditional relative frequency\n", "Let us throw two dice, a red and a blue one, and let *s* denotes the sum. What is the **relative frequency** (relative frequency of a conditional event) of 1 on the red die if *s* is given? What is the relative frequency of any other number on the red die if *s* is given? For which value of *s* will be this frequency close to 1/6?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from random import randint" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "two = [red, _] = [randint(1,6), randint(1,6)]\n", "two, red" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "two = [randint(1,6), randint(1,6)]\n", "red = two[0]\n", "two, red" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "s = sum(two)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "two, s, red" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum2 = 5 # sum of the two numbers (2..12)\n", "die = 1 # the number on the die we are waiting for (1..6)\n", "count = 0 # counter of the cases with sum == \n", "count_red = 0 # counter when the red die == \n", "\n", "for i in range(10000):\n", " two = [randint(1,6), randint(1,6)]\n", " red = two[0]\n", " s = sum(two)\n", " if s == sum2:\n", " count += 1\n", " if red == die:\n", " count_red += 1\n", "print(count_red/count, 1/6)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Conditional probability \n", "Let us throw two dice, a red and a blue one, and let *s* denotes the sum. What is the probability that the number *r* is on the red die if *s* is given? For which value of *s* will be this frequency equal to 1/6?" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "sum2 = 7 # sum of the two numbers \n", "die = 4 # the number on the die we are waiting for\n", "count = 0 # counter of the cases with sum == \n", "count_red = 0 # counter when the red die == \n", "\n", "for red in range(1, 7):\n", " for blue in range(1, 7):\n", " s = red + blue\n", " if s == sum2:\n", " count += 1\n", " if red == die:\n", " count_red += 1\n", "print(count_red/count, 1/6)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.12.3" } }, "nbformat": 4, "nbformat_minor": 4 }