{ "cells": [ { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "# İleri Seviye Kümeler (Sets)\n", "\n", "Bu konuda yeni bir veritipi olan kümeler veya ingilizce adıyla **setleri** öğreneceğiz.\n", "\n", "Kümeler, matematikte olduğu gibi bir elemandan sadece bir adet tutan bir veritipidir. Bu açıdan kullanıldıkları yerlerde çok önemli bir veritipi olmaktadırlar. İsterseniz hemen bir küme oluşturalım.\n", "\n", "### Küme oluşturmak" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = set() # Boş küme" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "set" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(x)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": true }, "outputs": [], "source": [ "liste = [1,2,3,3,1,1,2,2,2] # Aynı elemanı birçok defa barındıran bir liste" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = set(liste) # Veri tipi dönüşümü" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3}" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x # Aynı elemanlar tek bir elemana indirgendi." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = set(\"Python Programlama Dili\") # Aynı karakterler tek bir karaktere indirgendi." ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{' ', 'D', 'P', 'a', 'g', 'h', 'i', 'l', 'm', 'n', 'o', 'r', 't', 'y'}" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = {\"Python\",\"Php\",\"Python\"}" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'Php', 'Python'}" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x # Aynı elemanlar teke indirgendi." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### For döngüsüyle Gezinmek\n", "\n", "Kümeler de tıpkı sözlükler gibi sırasız bir veri tipidir. Bunu ***for*** döngüsüyle görebiliriz." ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = {\"Python\",\"Php\",\"Java\",\"C\",\"Javascript\"}" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Php\n", "Python\n", "Java\n", "Javascript\n", "C\n" ] } ], "source": [ "for i in x:\n", " print(i) " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Peki bir kümenin elemanlarına direk olarak ulaşabiliyor muyuz ? " ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{'C', 'Java', 'Javascript', 'Php', 'Python'}" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'set' object does not support indexing", "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[0mx\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[1;31mTypeError\u001b[0m: 'set' object does not support indexing" ] } ], "source": [ "x[0]" ] }, { "cell_type": "code", "execution_count": 20, "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[0mx\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"Python\"\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": [ "x[\"Python\"]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Buradaki işlemler aslında kümelerde tanımlı değil. Yani biz bir kümenin elemanlarına ne indexle ne de eleman ismiyle erişebiliyoruz. Erişmek için **mutlaka** veritipi dönüşümü yapmamız gerekiyor." ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "liste = list({1,2,3,4,5})\n", "\n", "liste[0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Kümelerin Metodları" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Eleman Eklemek : add() metodu\n", "\n", "Kümeye eleman eklemimizi sağlar. Aynı eleman eklenmeye çalışırsa hata vermez ve herhangi bir ekleme işlemi yapmaz." ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x = {1,2,3}" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x.add(4)" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4}" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": true }, "outputs": [], "source": [ "x.add(4) # Tekrar eklenmiyor." ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 3, 4}" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### İki kümenin farkı : difference() metodu\n", "\n", "Bu metod birinci kümenin ikinci kümeden farkını döner.\n", "\n", " küme1.difference(küme2) # Küme1'in Küme2'den farkı" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "collapsed": true }, "outputs": [], "source": [ "küme1 = {1,2,3,10,34,100,-2}\n", "küme2 = {1,2,23,34,-1}" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{-2, 3, 10, 100}" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme1.difference(küme2)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{-1, 23}" ] }, "execution_count": 36, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme2.difference(küme1)" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{-2, 1, 2, 3, 10, 34, 100}" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### İki kümenin farkı ve güncelleme : difference_update() metodu" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bu metod birinci kümenin ikinci kümeden farkını dönerek birinci kümeyi bu farka göre günceller.\n", "\n", " küme1.difference_update(küme2) # Küme1'in Küme2'den farkı" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{-2, 1, 2, 3, 10, 34, 100}" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme1" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{-1, 1, 2, 23, 34}" ] }, "execution_count": 40, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme2" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "collapsed": true }, "outputs": [], "source": [ "küme1.difference_update(küme2)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{-2, 3, 10, 100}" ] }, "execution_count": 43, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme1 # Farka göre güncellendi." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Kümeden Eleman Çıkartmak : discard() metodu\n", "\n", "İçine verilen değeri kümeden çıkartır. Eğer kümede öyle bir değer yoksa, bu metod hiçbir şey yapmaz(Hata vermez)." ] }, { "cell_type": "code", "execution_count": 47, "metadata": { "collapsed": true }, "outputs": [], "source": [ "küme1 = {1,2,3,4,5,6}" ] }, { "cell_type": "code", "execution_count": 48, "metadata": { "collapsed": true }, "outputs": [], "source": [ "küme1.discard(2)" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 3, 4, 5, 6}" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme1" ] }, { "cell_type": "code", "execution_count": 50, "metadata": { "collapsed": true }, "outputs": [], "source": [ "küme1.discard(10)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 3, 4, 5, 6}" ] }, "execution_count": 51, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Küme kesişimleri : intersection() metodu\n", "Bu metod iki kümenin kesişimleri bulmamızı sağlar.\n" ] }, { "cell_type": "code", "execution_count": 52, "metadata": { "collapsed": true }, "outputs": [], "source": [ "küme1 = {1,2,3,10,34,100,-2}\n", "küme2 = {1,2,23,34,-1}" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 34}" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme1.intersection(küme2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Küme kesişimleri ve güncelleme : intersection_update() metodu" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Bu metod birinci kümeyle ikinci kümenin kesişimlerini bulur ve birinci kümeyi bu kesişime göre günceller." ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "collapsed": true }, "outputs": [], "source": [ "küme1 = {1,2,3,10,34,100,-2}\n", "küme2 = {1,2,23,34,-1}" ] }, { "cell_type": "code", "execution_count": 56, "metadata": { "collapsed": true }, "outputs": [], "source": [ "küme1.intersection_update(küme2)" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{1, 2, 34}" ] }, "execution_count": 57, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Kümeler ayrık küme mi ? : isdisjoint() metodu\n", "\n", "Bu metod, eğer iki kümenin kesişim kümesi boş ise True, değilse False döner." ] }, { "cell_type": "code", "execution_count": 61, "metadata": { "collapsed": true }, "outputs": [], "source": [ "küme1 = {1,2,3,10,34,100,-2}\n", "küme2 = {1,2,23,34,-1}\n", "küme3 = {30,40,50}" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 62, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme1.isdisjoint(küme2)" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme1.isdisjoint(küme3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Alt kümesi mi ? : issubset() metodu\n", "\n", "Bu metod , birinci küme ikinci kümenin alt kümesiyse True, değilse False döner." ] }, { "cell_type": "code", "execution_count": 64, "metadata": { "collapsed": true }, "outputs": [], "source": [ "küme1 = {1,2,3}\n", "küme2 = {1,2,3,4}\n", "küme3 = {5,6,7}" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 65, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme1.issubset(küme2)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme1.issubset(küme3)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Birleşim Kümesi : union() metodu\n", "\n", "Bu metod, iki kümenin birleşim kümesini döner." ] }, { "cell_type": "code", "execution_count": 67, "metadata": { "collapsed": true }, "outputs": [], "source": [ "küme1 = {1,2,3,10,34,100,-2}\n", "küme2 = {1,2,23,34,-1}" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{-2, -1, 1, 2, 3, 10, 23, 34, 100}" ] }, "execution_count": 68, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme1.union(küme2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Birleşim Kümesi ve update : update() metodu\n", "\n", "Bu birinci kümeyle ikinci kümenin birleşim kümesini döner ve birinci kümeyi buna göre günceller." ] }, { "cell_type": "code", "execution_count": 72, "metadata": { "collapsed": true }, "outputs": [], "source": [ "küme1 = {1,2,3,10,34,100,-2}\n", "küme2 = {1,2,23,34,-1}\n", "\n", "küme1.update(küme2)" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "{-2, -1, 1, 2, 3, 10, 23, 34, 100}" ] }, "execution_count": 73, "metadata": {}, "output_type": "execute_result" } ], "source": [ "küme1" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "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.6.1" } }, "nbformat": 4, "nbformat_minor": 2 }