{ "cells": [ { "cell_type": "markdown", "id": "a0d6f34d", "metadata": {}, "source": [ "A *set* is an un-ordered collection of *distinct* objects. \n", "- Sets are like dictionaries except with only keys, no values. \n", "- Like dictionaries, sets can only contain mutable (hashable objects). \n", "- Checking if an item is in a set is fast, regardless of the size of the set." ] }, { "cell_type": "code", "execution_count": 1, "id": "70ad56eb", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s = {1, 2, 3, 4, 5}\n", "3 in s" ] }, { "cell_type": "code", "execution_count": 4, "id": "940fb1f8", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{3, 4, 5, 6, 7}" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# uniqueness is inforced \n", "t = {3, 4, 5, 6, 6, 6, 7}\n", "t" ] }, { "cell_type": "code", "execution_count": 6, "id": "e7c3d646", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{3, 4, 5}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# intersection\n", "s & t" ] }, { "cell_type": "code", "execution_count": 7, "id": "ed39ffe3", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 5, 6, 7}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# union\n", "s | t" ] }, { "cell_type": "code", "execution_count": 9, "id": "6bec4721", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{1, 2, 3, 4, 5}\n", "{3, 4, 5, 6, 7}\n", "{1, 2}\n", "{6, 7}\n" ] } ], "source": [ "# set difference\n", "print(s)\n", "print(t)\n", "print(s - t)\n", "print(t - s)\n" ] }, { "cell_type": "code", "execution_count": 12, "id": "670b668a", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 6, 7}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# set symmetric difference \n", "s ^ t" ] }, { "cell_type": "code", "execution_count": 13, "id": "db3de92e", "metadata": {}, "outputs": [], "source": [ "# compare the speed of finding an item in a list\n", "big_list = list(range(10**8))\n" ] }, { "cell_type": "code", "execution_count": 20, "id": "b354b127", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "-1 in big_list" ] }, { "cell_type": "code", "execution_count": 17, "id": "c64263bd", "metadata": {}, "outputs": [], "source": [ "big_set = set(big_list)" ] }, { "cell_type": "code", "execution_count": 21, "id": "ea46ab8b", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "-1 in big_set" ] }, { "cell_type": "code", "execution_count": null, "id": "43ae95ce", "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.9.13" } }, "nbformat": 4, "nbformat_minor": 5 }