Skip to content

Instantly share code, notes, and snippets.

@mde-2590
Created April 6, 2019 19:39
Show Gist options
  • Save mde-2590/686b451a32f771eaf1a5712063f4ae76 to your computer and use it in GitHub Desktop.
Save mde-2590/686b451a32f771eaf1a5712063f4ae76 to your computer and use it in GitHub Desktop.
Created on Cognitive Class Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"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": [
"# Create a set\n",
"\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": [
"# Convert list to set\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": [
"# Convert list to set\n",
"\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": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'Thriller'}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Sample set\n",
"\n",
"A = set([\"Thriller\", \"Back in Black\", \"AC/DC\"])\n",
"A"
]
},
{
"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": [
"# Add element to set\n",
"\n",
"A.add(\"NSYNC\")\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'NSYNC', 'Thriller'}"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Try to add duplicate element to the set\n",
"\n",
"A.add(\"NSYNC\")\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'Thriller'}"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Remove the element from set\n",
"\n",
"A.remove(\"NSYNC\")\n",
"A"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Verify if the element is in the set\n",
"\n",
"\"AC/DC\" in A"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Verify if the element is in the set\n",
"\n",
"\"Who\" in A"
]
},
{
"cell_type": "code",
"execution_count": 11,
"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": 11,
"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",
"\n",
"# Print two sets\n",
"\n",
"album_set1, album_set2"
]
},
{
"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": [
"# Find the intersections\n",
"\n",
"intersection = album_set1 & album_set2\n",
"intersection"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Thriller'}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Find the difference in set1 but not set2\n",
"\n",
"album_set1.difference(album_set2) "
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'The Dark Side of the Moon'}"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"album_set2.difference(album_set1) "
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black'}"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Use intersection method to find the intersection of album_list1 and album_list2\n",
"\n",
"album_set1.intersection(album_set2) "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'The Dark Side of the Moon', 'Thriller'}"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Find the union of two sets\n",
"\n",
"album_set1.union(album_set2)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if superset\n",
"\n",
"set(album_set1).issuperset(album_set2)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if subset\n",
"\n",
"set(album_set2).issubset(album_set1) "
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if subset\n",
"\n",
"set({\"Back in Black\", \"AC/DC\"}).issubset(album_set1) "
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Check if superset\n",
"\n",
"album_set1.issuperset({\"Back in Black\", \"AC/DC\"}) "
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['rap', 'house', 'electronic music', 'rap']"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Convert the list ['rap','house','electronic music', 'rap'] to a set:\n",
"\n",
"set1 = (['rap','house','electronic music', 'rap'])\n",
"set1"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"the sum of A is: 6\n",
"the sum of B is: 3\n"
]
}
],
"source": [
"#Consider the list A = [1, 2, 2, 1] and set B = set([1, 2, 2, 1]), does sum(A) = sum(B)\n",
"\n",
"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": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'AC/DC', 'Back in Black', 'The Dark Side of the Moon', 'Thriller'}"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Create a new set album_set3 that is the union of album_set1 and album_set2:\n",
"\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": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Find out if album_set1 is a subset of album_set3:\n",
"\n",
"album_set1.issubset(album_set3)\n"
]
},
{
"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.6.8"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment