Skip to content

Instantly share code, notes, and snippets.

@salamituns
Created January 29, 2021 14:32
Show Gist options
  • Save salamituns/eded04603ee4216060a7fc470717b6fa to your computer and use it in GitHub Desktop.
Save salamituns/eded04603ee4216060a7fc470717b6fa to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sets: {}\n",
"They are unordered unlike Lists, Tuples, and Dictionary.\n",
"They can also contain different types of python data types.\n",
"They only have unique elements i.e:, one unique elements can occur in a set."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'R&B', 'disco', 'hard rock', 'pop', 'rock', 'soul'}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#creating a set.\n",
"set1 = {\"pop\", \"rock\", \"soul\", \"hard rock\", \"rock\", \"R&B\", \"rock\", \"disco\"}\n",
"set1"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'00:42:19',\n",
" 10.0,\n",
" 1982,\n",
" '30-Nov-82',\n",
" 46.0,\n",
" 65,\n",
" 'Michael Jackson',\n",
" None,\n",
" 'Pop, Rock, R&B',\n",
" 'Thriller'}"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#We can also create a set from a list as follows:\n",
"\n",
"album_list = [ \"Michael Jackson\", \"Thriller\", 1982, \"00:42:19\", \\\n",
" \"Pop, Rock, R&B\", 46.0, 65, \"30-Nov-82\", None, 10.0]\n",
"album_set = set(album_list) \n",
"album_set"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'R&B',\n",
" 'disco',\n",
" 'folk rock',\n",
" 'hard rock',\n",
" 'pop',\n",
" 'progressive rock',\n",
" 'rock',\n",
" 'soft rock',\n",
" 'soul'}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Just creating a set of Genre from the list of genre.\n",
"music_genres = set([\"pop\", \"pop\", \"rock\", \"folk rock\", \"hard rock\", \"soul\", \\\n",
" \"progressive rock\", \"soft rock\", \"R&B\", \"disco\"])\n",
"music_genres"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Set Operations."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'Thriller'}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = set([\"Thriller\", \"Back in Black\", \"AC/DC\"])\n",
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Add element to set using the .add function."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'NSYNC', 'Thriller'}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A.add(\"NSYNC\")\n",
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can remove element from set using the .remove function"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'Thriller'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A.remove(\"NSYNC\")\n",
"A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can verify if an element is in the set using the 'in' command:"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'AC/DC' in A"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Sets Logic Operations"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"({'AC/DC', 'Back in Black', 'Thriller'},\n",
" {'AC/DC', 'Back in Black', 'The Dark Side of the Moon'})"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Sample sets\n",
"\n",
"album_set1 = set([\"Thriller\", 'AC/DC', 'Back in Black'])\n",
"album_set2 = set([ \"AC/DC\", \"Back in Black\", \"The Dark Side of the Moon\"])\n",
"album_set1 , album_set2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Take note that both sets :album_set1 , album_set2 contains similar elements: AC/DC, Back in Black."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black'}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#We can find the intersection of both sets using the & function in python.\n",
"\n",
"intersection = album_set1 & album_set2\n",
"intersection"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" OR Using the Intersection function."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black'}"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"album_set1.intersection(album_set2) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can find all the elements that are only contained in album_set1 using the difference method:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We only need to consider elements in album_set1; all the elements in album_set2, including the intersection, are not included."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Thriller'}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#To find the difference in set1 but not set2\n",
"\n",
"album_set1.difference(album_set2) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Union.\n",
"Union corresponds to all the elements in both sets"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'The Dark Side of the Moon', 'Thriller'}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#We can find the union of two sets using the .union() function in python\n",
"\n",
"album_set1.union(album_set2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also check if a set is a superset or subset of another set, respectively, like this:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Check if superset\n",
"\n",
"set(album_set1).issuperset(album_set2) "
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if superset\n",
"\n",
"album_set1.issuperset({\"Back in Black\", \"AC/DC\"}) "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if subset\n",
"\n",
"set(album_set2).issubset(album_set1)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if subset\n",
"\n",
"set({\"Back in Black\", \"AC/DC\"}).issubset(album_set1) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tasks on set"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'electronic music', 'house', 'rap'}"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Convert the list to a set.\n",
"music_kind = set(['rap','house','electronic music', 'rap'])\n",
"music_kind"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The sum of A is: 6\n",
"The sum of B is: 3\n"
]
}
],
"source": [
"A = [1, 2, 2, 1]\n",
"B = set([1, 2, 2, 1])\n",
"print ('The sum of A is:', sum(A))\n",
"print ('The sum of B is:', sum(B))"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'The Dark Side of the Moon', 'Thriller'}"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Create a new set album_set3 that is the union of album_set1 and album_set2:\n",
"\n",
"album_set1 = set([\"Thriller\", 'AC/DC', 'Back in Black'])\n",
"album_set2 = set([ \"AC/DC\", \"Back in Black\", \"The Dark Side of the Moon\"])\n",
"album_set3 = album_set1.union(album_set2)\n",
"album_set3"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Find out if album_set1 is a subset of album_set3:\n",
"\n",
"album_set1.issubset(album_set3)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment