Created
December 21, 2018 14:29
-
-
Save Chrislu30604/cd29001f5b3c06f233e73c499cc16ca9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"# Set" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### What' s set ?\n", | |
"- set([iterable])\n", | |
"- iterable (Optional) - a sequence (string, tuple, etc.) or collection (set, dictionary, etc.) or an iterator object to be converted into a set\n", | |
"- unorder collection item ---set---> unique(no duplicates) and must be immutable item" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Example : create set sequence" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"set()\n", | |
"{'y', 'n', 'P', 't', 'o', 'h'}\n", | |
"{'a', 'i', 'o', 'u', 'e'}\n", | |
"{'a', 'i', 'o', 'u', 'e'}\n", | |
"{0, 1, 2, 3, 4}\n" | |
] | |
} | |
], | |
"source": [ | |
"# empty set\n", | |
"print(set())\n", | |
"\n", | |
"# from string\n", | |
"print(set('Python'))\n", | |
"\n", | |
"# from tuple\n", | |
"print(set(('a', 'e', 'i', 'o', 'u')))\n", | |
"\n", | |
"# from list\n", | |
"print(set(['a', 'e', 'i', 'o', 'u']))\n", | |
"\n", | |
"# from range\n", | |
"print(set(range(5)))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Example : Create set from dictionary and frozen set" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'a', 'o', 'i', 'u', 'e'}\n", | |
"{'a', 'o', 'u', 'i', 'e'}\n", | |
"{'a', 'o', 'i', 'u', 'e'}\n" | |
] | |
} | |
], | |
"source": [ | |
"# for set\n", | |
"print(set({'a', 'e', 'i', 'o', 'u'}))\n", | |
"\n", | |
"# from dictionary\n", | |
"print(set({'a':1, 'e': 2, 'i':3, 'o':4, 'u':5}))\n", | |
"\n", | |
"# from frozen set\n", | |
"frozenSet = frozenset(('a', 'e', 'i', 'o', 'u'))\n", | |
"print(set(frozenSet))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Example : From custom iterable object ?" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{1, 2, 3, 4, 5}\n" | |
] | |
} | |
], | |
"source": [ | |
"class PrintNumber:\n", | |
" def __init__(self, max):\n", | |
" self.max = max\n", | |
"\n", | |
" def __iter__(self):\n", | |
" self.num = 0\n", | |
" return self\n", | |
"\n", | |
" def __next__(self):\n", | |
" if(self.num >= self.max):\n", | |
" raise StopIteration\n", | |
" self.num += 1\n", | |
" return self.num\n", | |
"\n", | |
"printNum = PrintNumber(5)\n", | |
"\n", | |
"# creating a set\n", | |
"print(set(printNum))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Example : discard and remove" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{1, 3, 4, 5, 6}\n", | |
"{1, 3, 5, 6}\n", | |
"{1, 3, 5}\n", | |
"{1, 3, 5}\n" | |
] | |
} | |
], | |
"source": [ | |
"my_set = {1, 3, 4, 5, 6}\n", | |
"print(my_set)\n", | |
"# discard an element\n", | |
"# Output: {1, 3, 5, 6}\n", | |
"my_set.discard(4)\n", | |
"print(my_set)\n", | |
"\n", | |
"# remove an element\n", | |
"# Output: {1, 3, 5}\n", | |
"my_set.remove(6)\n", | |
"print(my_set)\n", | |
"\n", | |
"# discard an element\n", | |
"# not present in my_set\n", | |
"# Output: {1, 3, 5}\n", | |
"my_set.discard(2)\n", | |
"print(my_set)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Example : Pop and Clear" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{'d', 'H', 'l', 'o', 'W', 'e', 'r'}\n", | |
"d\n", | |
"{'l', 'o', 'W', 'e', 'r'}\n", | |
"set()\n" | |
] | |
} | |
], | |
"source": [ | |
"my_set = set(\"HelloWorld\")\n", | |
"print(my_set)\n", | |
"\n", | |
"# pop an element\n", | |
"# Output: random element\n", | |
"print(my_set.pop())\n", | |
"\n", | |
"# pop another element\n", | |
"# Output: random element\n", | |
"my_set.pop()\n", | |
"print(my_set)\n", | |
"\n", | |
"# clear my_set\n", | |
"#Output: set()\n", | |
"my_set.clear()\n", | |
"print(my_set)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Example : Union" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{1, 2, 3, 4, 5, 6, 7, 8}\n", | |
"{1, 2, 3, 4, 5, 6, 7, 8}\n", | |
"{1, 2, 3, 4, 5, 6, 7, 8}\n" | |
] | |
} | |
], | |
"source": [ | |
"# initialize A and B\n", | |
"A = {1, 2, 3, 4, 5}\n", | |
"B = {4, 5, 6, 7, 8}\n", | |
"\n", | |
"# use | operator\n", | |
"# Output: {1, 2, 3, 4, 5, 6, 7, 8}\n", | |
"print(A | B)\n", | |
"print(A.union(B))\n", | |
"print(B.union(A))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Example : Intersection" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 8, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{4, 5}\n" | |
] | |
}, | |
{ | |
"data": { | |
"text/plain": [ | |
"{4, 5}" | |
] | |
}, | |
"execution_count": 8, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"# initialize A and B\n", | |
"A = {1, 2, 3, 4, 5}\n", | |
"B = {4, 5, 6, 7, 8}\n", | |
"\n", | |
"# use & operator\n", | |
"# Output: {4, 5}\n", | |
"print(A & B)\n", | |
"print(A.intersection(B))\n", | |
"print(B.intersection(A))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Example : Difference" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 10, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{1, 2, 3}\n", | |
"{1, 2, 3}\n", | |
"{8, 6, 7}\n" | |
] | |
} | |
], | |
"source": [ | |
"# initialize A and B\n", | |
"A = {1, 2, 3, 4, 5}\n", | |
"B = {4, 5, 6, 7, 8}\n", | |
"\n", | |
"# use - operator on A\n", | |
"# Output: {1, 2, 3}\n", | |
"print(A - B)\n", | |
"print(A.difference(B))\n", | |
"print(B.difference(A))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Example : Symmetric Difference" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 11, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"{1, 2, 3, 6, 7, 8}\n", | |
"{1, 2, 3, 6, 7, 8}\n", | |
"{1, 2, 3, 6, 7, 8}\n" | |
] | |
} | |
], | |
"source": [ | |
"# initialize A and B\n", | |
"A = {1, 2, 3, 4, 5}\n", | |
"B = {4, 5, 6, 7, 8}\n", | |
"\n", | |
"# use ^ operator\n", | |
"# Output: {1, 2, 3, 6, 7, 8}\n", | |
"print(A ^ B)\n", | |
"print(A.symmetric_difference(B))\n", | |
"print(B.symmetric_difference(A))" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Example : Test Member" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 12, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"True\n", | |
"False\n" | |
] | |
} | |
], | |
"source": [ | |
"# initialize my_set\n", | |
"my_set = set(\"apple\")\n", | |
"\n", | |
"# check if 'a' is present\n", | |
"# Output: True\n", | |
"print('a' in my_set)\n", | |
"\n", | |
"# check if 'p' is present\n", | |
"# Output: False\n", | |
"print('p' not in my_set)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"### Example : Iterating through a Set" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": { | |
"scrolled": true | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"a\n", | |
"e\n", | |
"p\n", | |
"l\n" | |
] | |
} | |
], | |
"source": [ | |
"for letter in set(\"apple\"):\n", | |
" print(letter)" | |
] | |
} | |
], | |
"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.5" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment