Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save binarygalwalkin/929d590bc3c544ca7a66d8992cbb5453 to your computer and use it in GitHub Desktop.
Save binarygalwalkin/929d590bc3c544ca7a66d8992cbb5453 to your computer and use it in GitHub Desktop.
Dictionaries & sets
Display the source blob
Display the rendered blob
Raw
{
"nbformat_minor": 1,
"cells": [
{
"execution_count": 1,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 1,
"metadata": {},
"data": {
"text/plain": "{(0, 1): 6,\n 'key5': 5,\n 'key1': 1,\n 'key3': [3, 3, 3],\n 'key2': '2',\n 'key4': (4, 4, 4)}"
},
"output_type": "execute_result"
}
],
"source": "# Create the dictionary\n\nDict = {\"key1\": 1, \"key2\": \"2\", \"key3\": [3, 3, 3], \"key4\": (4, 4, 4), ('key5'): 5, (0, 1): 6}\nDict"
},
{
"execution_count": 2,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 2,
"metadata": {},
"data": {
"text/plain": "1"
},
"output_type": "execute_result"
}
],
"source": "# Access to the value by the key\n\nDict[\"key1\"]"
},
{
"execution_count": 3,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 3,
"metadata": {},
"data": {
"text/plain": "6"
},
"output_type": "execute_result"
}
],
"source": "# Access to the value by the key\n\nDict[(0, 1)]"
},
{
"execution_count": 4,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 4,
"metadata": {},
"data": {
"text/plain": "{'Back in Black': '1980',\n 'Bat Out of Hell': '1977',\n 'Rumours': '1977',\n 'Saturday Night Fever': '1977',\n 'The Bodyguard': '1992',\n 'The Dark Side of the Moon': '1973',\n 'Their Greatest Hits (1971-1975)': '1976',\n 'Thriller': '1982'}"
},
"output_type": "execute_result"
}
],
"source": "# Create a sample dictionary\n\nrelease_year_dict = {\"Thriller\": \"1982\", \"Back in Black\": \"1980\", \\\n \"The Dark Side of the Moon\": \"1973\", \"The Bodyguard\": \"1992\", \\\n \"Bat Out of Hell\": \"1977\", \"Their Greatest Hits (1971-1975)\": \"1976\", \\\n \"Saturday Night Fever\": \"1977\", \"Rumours\": \"1977\"}\nrelease_year_dict"
},
{
"execution_count": 5,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 5,
"metadata": {},
"data": {
"text/plain": "'1982'"
},
"output_type": "execute_result"
}
],
"source": "# Get value by keys\n\nrelease_year_dict['Thriller'] "
},
{
"execution_count": 6,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 6,
"metadata": {},
"data": {
"text/plain": "dict_keys(['Thriller', 'The Dark Side of the Moon', 'The Bodyguard', 'Saturday Night Fever', 'Back in Black', 'Bat Out of Hell', 'Rumours', 'Their Greatest Hits (1971-1975)'])"
},
"output_type": "execute_result"
}
],
"source": "# Get all the keys in dictionary\n\nrelease_year_dict.keys() "
},
{
"execution_count": 7,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 7,
"metadata": {},
"data": {
"text/plain": "dict_values(['1982', '1973', '1992', '1977', '1980', '1977', '1977', '1976'])"
},
"output_type": "execute_result"
}
],
"source": "# Get all the values in dictionary\n\nrelease_year_dict.values() "
},
{
"execution_count": 8,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 8,
"metadata": {},
"data": {
"text/plain": "{'Back in Black': '1980',\n 'Bat Out of Hell': '1977',\n 'Graduation': '2007',\n 'Rumours': '1977',\n 'Saturday Night Fever': '1977',\n 'The Bodyguard': '1992',\n 'The Dark Side of the Moon': '1973',\n 'Their Greatest Hits (1971-1975)': '1976',\n 'Thriller': '1982'}"
},
"output_type": "execute_result"
}
],
"source": "# Append value with key into dictionary\n\nrelease_year_dict['Graduation'] = '2007'\nrelease_year_dict"
},
{
"execution_count": 9,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 9,
"metadata": {},
"data": {
"text/plain": "{'Back in Black': '1980',\n 'Bat Out of Hell': '1977',\n 'Rumours': '1977',\n 'Saturday Night Fever': '1977',\n 'The Bodyguard': '1992',\n 'The Dark Side of the Moon': '1973',\n 'Their Greatest Hits (1971-1975)': '1976'}"
},
"output_type": "execute_result"
}
],
"source": "# Delete entries by key\n\ndel(release_year_dict['Thriller'])\ndel(release_year_dict['Graduation'])\nrelease_year_dict"
},
{
"execution_count": 10,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 10,
"metadata": {},
"data": {
"text/plain": "True"
},
"output_type": "execute_result"
}
],
"source": "# Verify the key is in the dictionary\n\n'The Bodyguard' in release_year_dict"
},
{
"execution_count": 11,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 11,
"metadata": {},
"data": {
"text/plain": "{'Saturday Night Fever': '1977', 'The Bodyguard': '1992'}"
},
"output_type": "execute_result"
}
],
"source": "# Question sample dictionary\n\nsoundtrack_dic = {\"The Bodyguard\":\"1992\", \"Saturday Night Fever\":\"1977\"}\nsoundtrack_dic "
},
{
"execution_count": 13,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 13,
"metadata": {},
"data": {
"text/plain": "dict_keys(['The Bodyguard', 'Saturday Night Fever'])"
},
"output_type": "execute_result"
}
],
"source": "#In the dictionary soundtrack_dict what are the keys ?\n\nsoundtrack_dic.keys()"
},
{
"execution_count": 14,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 14,
"metadata": {},
"data": {
"text/plain": "dict_values(['1992', '1977'])"
},
"output_type": "execute_result"
}
],
"source": "#In the dictionary soundtrack_dict what are the values ?\n\nsoundtrack_dic.values()"
},
{
"execution_count": 25,
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": "\n#The Albums Back in Black, The Bodyguard and Thriller have the following music recording sales in millions 50, 50 and 65 respectively:\n#Create a dictionary album_sales_dict where the keys are the album name and the sales in millions are the values. \n\nalbum_sales_dict={'Back in Black':50,'The Bodyguard':50,'Thriller':65}"
},
{
"execution_count": 18,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 18,
"metadata": {},
"data": {
"text/plain": "{'Back in Black': 50, 'The Bodyguard': 50, 'Thriller': 65}"
},
"output_type": "execute_result"
}
],
"source": "album_sales_dict"
},
{
"execution_count": 21,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 21,
"metadata": {},
"data": {
"text/plain": "65"
},
"output_type": "execute_result"
}
],
"source": "album_sales_dict['Thriller']"
},
{
"execution_count": 23,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 23,
"metadata": {},
"data": {
"text/plain": "dict_keys(['Thriller', 'The Bodyguard', 'Back in Black'])"
},
"output_type": "execute_result"
}
],
"source": "album_sales_dict.keys()"
},
{
"execution_count": 24,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 24,
"metadata": {},
"data": {
"text/plain": "dict_values([65, 50, 50])"
},
"output_type": "execute_result"
}
],
"source": "album_sales_dict.values()"
},
{
"execution_count": 26,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 26,
"metadata": {},
"data": {
"text/plain": "{'R&B', 'disco', 'hard rock', 'pop', 'rock', 'soul'}"
},
"output_type": "execute_result"
}
],
"source": "# Create a set\n\nset1 = {\"pop\", \"rock\", \"soul\", \"hard rock\", \"rock\", \"R&B\", \"rock\", \"disco\"}\nset1"
},
{
"execution_count": 27,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 27,
"metadata": {},
"data": {
"text/plain": "{'Thriller',\n 65,\n 10.0,\n 46.0,\n None,\n 'Pop, Rock, R&B',\n '00:42:19',\n '30-Nov-82',\n 'Michael Jackson',\n 1982}"
},
"output_type": "execute_result"
}
],
"source": "# Convert list to set\n\nalbum_list = [ \"Michael Jackson\", \"Thriller\", 1982, \"00:42:19\", \\\n \"Pop, Rock, R&B\", 46.0, 65, \"30-Nov-82\", None, 10.0]\nalbum_set = set(album_list) \nalbum_set"
},
{
"execution_count": 28,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 28,
"metadata": {},
"data": {
"text/plain": "{'AC/DC', 'Back in Black', 'Thriller'}"
},
"output_type": "execute_result"
}
],
"source": "# Sample set\n\nA = set([\"Thriller\", \"Back in Black\", \"AC/DC\"])\nA"
},
{
"execution_count": 29,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 29,
"metadata": {},
"data": {
"text/plain": "{'AC/DC', 'Back in Black', 'NSYNC', 'Thriller'}"
},
"output_type": "execute_result"
}
],
"source": "# Add element to set\n\nA.add(\"NSYNC\")\nA"
},
{
"execution_count": 30,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 30,
"metadata": {},
"data": {
"text/plain": "{'AC/DC', 'Back in Black', 'NSYNC', 'Thriller'}"
},
"output_type": "execute_result"
}
],
"source": "# Try to add duplicate element to the set\n\nA.add(\"NSYNC\")\nA"
},
{
"execution_count": 31,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 31,
"metadata": {},
"data": {
"text/plain": "{'AC/DC', 'Back in Black', 'Thriller'}"
},
"output_type": "execute_result"
}
],
"source": "# Remove the element from set\n\nA.remove(\"NSYNC\")\nA"
},
{
"execution_count": 32,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 32,
"metadata": {},
"data": {
"text/plain": "True"
},
"output_type": "execute_result"
}
],
"source": "# Verify if the element is in the set\n\n\"AC/DC\" in A"
},
{
"execution_count": 33,
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": "# Sample Sets\n\nalbum_set1 = set([\"Thriller\", 'AC/DC', 'Back in Black'])\nalbum_set2 = set([ \"AC/DC\", \"Back in Black\", \"The Dark Side of the Moon\"])"
},
{
"execution_count": 34,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 34,
"metadata": {},
"data": {
"text/plain": "({'AC/DC', 'Back in Black', 'Thriller'},\n {'AC/DC', 'Back in Black', 'The Dark Side of the Moon'})"
},
"output_type": "execute_result"
}
],
"source": "# Print two sets\n\nalbum_set1, album_set2"
},
{
"execution_count": 35,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 35,
"metadata": {},
"data": {
"text/plain": "{'AC/DC', 'Back in Black'}"
},
"output_type": "execute_result"
}
],
"source": "# Find the intersections\n\nintersection = album_set1 & album_set2\nintersection"
},
{
"execution_count": 36,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 36,
"metadata": {},
"data": {
"text/plain": "{'Thriller'}"
},
"output_type": "execute_result"
}
],
"source": "# Find the difference in set1 but not set2\n\nalbum_set1.difference(album_set2) "
},
{
"execution_count": 37,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 37,
"metadata": {},
"data": {
"text/plain": "{'AC/DC', 'Back in Black'}"
},
"output_type": "execute_result"
}
],
"source": "# Use intersection method to find the intersection of album_list1 and album_list2\n\nalbum_set1.intersection(album_set2) "
},
{
"execution_count": 38,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 38,
"metadata": {},
"data": {
"text/plain": "{'AC/DC', 'Back in Black', 'The Dark Side of the Moon', 'Thriller'}"
},
"output_type": "execute_result"
}
],
"source": "# Find the union of two sets\n\nalbum_set1.union(album_set2)"
},
{
"execution_count": 39,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 39,
"metadata": {},
"data": {
"text/plain": "False"
},
"output_type": "execute_result"
}
],
"source": "# Check if superset\n\nset(album_set1).issuperset(album_set2) "
},
{
"execution_count": 40,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 40,
"metadata": {},
"data": {
"text/plain": "False"
},
"output_type": "execute_result"
}
],
"source": "# Check if subset\n\nset(album_set2).issubset(album_set1) "
},
{
"execution_count": 41,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 41,
"metadata": {},
"data": {
"text/plain": "True"
},
"output_type": "execute_result"
}
],
"source": "# Check if subset\n\nset({\"Back in Black\", \"AC/DC\"}).issubset(album_set1) "
},
{
"execution_count": 42,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 42,
"metadata": {},
"data": {
"text/plain": "True"
},
"output_type": "execute_result"
}
],
"source": "# Check if superset\n\nalbum_set1.issuperset({\"Back in Black\", \"AC/DC\"}) "
},
{
"execution_count": 46,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 46,
"metadata": {},
"data": {
"text/plain": "['rap', 'house', 'electronic music', 'rap']"
},
"output_type": "execute_result"
}
],
"source": "#Convert the list ['rap','house','electronic music', 'rap'] to a set:\n\nset=(['rap','house','electronic music', 'rap'])\nset"
},
{
"execution_count": 52,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": "the sum of A is: 6\nthe sum of B is: 6\n"
}
],
"source": "#Consider the list A = [1, 2, 2, 1] and set B = set([1, 2, 2, 1]), does sum(A) = sum(B) \n\nA = [1, 2, 2, 1] \nB =([1, 2, 2, 1])\nprint(\"the sum of A is:\", sum(A))\nprint(\"the sum of B is:\", sum(B))\n"
},
{
"execution_count": 56,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 56,
"metadata": {},
"data": {
"text/plain": "{'AC/DC', 'Back in Black', 'The Dark Side of the Moon', 'Thriller'}"
},
"output_type": "execute_result"
}
],
"source": "#Create a new set album_set3 that is the union of album_set1 and album_set2:\n\nalbum_set3=album_set1.union(album_set2)\nalbum_set3"
},
{
"execution_count": 59,
"cell_type": "code",
"metadata": {},
"outputs": [
{
"execution_count": 59,
"metadata": {},
"data": {
"text/plain": "True"
},
"output_type": "execute_result"
}
],
"source": "#Find out if album_set1 is a subset of album_set3:\n\nalbum_set1.issubset(album_set3)"
},
{
"execution_count": null,
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": ""
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.5",
"name": "python3",
"language": "python"
},
"language_info": {
"mimetype": "text/x-python",
"nbconvert_exporter": "python",
"version": "3.5.5",
"name": "python",
"file_extension": ".py",
"pygments_lexer": "ipython3",
"codemirror_mode": {
"version": 3,
"name": "ipython"
}
}
},
"nbformat": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment