{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Множества" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "неотсортированная коллекция уникальных элементов" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "поддерживают итерацию, добавление и удаление объектов и т.д." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " индексация и срезы в сетах не поддерживаются" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{'d', 'b', 'e', 'a', 'c'}\n", "{'j', 'g', 'i', 'a', 'h'}\n" ] } ], "source": [ "s1 = set('abacaaaade') \n", "s2 = set('aghij') \n", "print(s1)\n", "print(s2)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'b', 'c', 'd', 'e'}" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s3 = s1 - s2 \n", "s3" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a', 'b', 'c', 'd', 'e', 'g', 'h', 'i', 'j'}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s3 = s1 | s2 \n", "s3 \n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'a'}" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s3 = s1 & s2 \n", "s3\n" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{6, 'a', 'b', 'c', 'd', 'e'}" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1.add(6) \n", "s1" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'b', 'c', 'd', 'e'}" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "s1.remove(6) \n", "s1" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "d\n", "b\n", "e\n", "c\n" ] } ], "source": [ "for item in s1:\n", " print(item) " ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4, 6, 7}" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = [1, 2, 3, 4, 1, 2, 6, 7] \n", "set(L) " ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 4, 6, 7]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "L = sorted(list(set(L)))\n", "L" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0, 6, 12, 18}" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "div2 = [x * 2 for x in range(0, 11)]\n", "div3 = [x * 3 for x in range(0, 7)]\n", "\n", "div2_set = set(div2) \n", "div3_set = set(div3) \n", "\n", "div2_set & div3_set" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{0, 2, 3, 4, 6, 8, 9, 10, 12, 14, 15, 16, 18, 20}" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "div2_set | div3_set" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{2, 4, 8, 10, 14, 16, 20}" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "div2_set - div3_set" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'set' object is not subscriptable", "output_type": "error", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mdiv2_set\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: 'set' object is not subscriptable" ] } ], "source": [ "div2_set[0]" ] }, { "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.7.5" } }, "nbformat": 4, "nbformat_minor": 2 }