Skip to content

Instantly share code, notes, and snippets.

@letscode0410
Created December 13, 2018 04:57
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 letscode0410/d163b0dab49dcaa1f42f6c4595154319 to your computer and use it in GitHub Desktop.
Save letscode0410/d163b0dab49dcaa1f42f6c4595154319 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": "markdown",
"metadata": {},
"source": [
" <div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <a href=\"http://cocl.us/NotebooksPython101\"><img src = \"https://ibm.box.com/shared/static/yfe6h4az47ktg2mm9h05wby2n7e8kei3.png\" width = 750, align = \"center\"></a>\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a href=\"https://www.bigdatauniversity.com\"><img src = \"https://ibm.box.com/shared/static/ugcqz6ohbvff804xp84y4kqnvvk3bq1g.png\" width = 300, align = \"center\"></a>\n",
"\n",
"<h1 align=center><font size = 5>Sets and Dictionaries</font></h1>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"## Table of Contents\n",
"\n",
"\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
"\n",
"<li><a href=\"#ref2\">Dictionaries</a></li>\n",
"<br>\n",
"<p></p>\n",
"Estimated Time Needed: <strong>20 min</strong>\n",
"</div>\n",
"\n",
"<hr>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id=\"ref2\"></a>\n",
"<h2 align=center> Dictionaries in Python </h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A dictionary consists of keys and values. It is helpful to compare a dictionary to a list. Instead of the numerical indexes such as a list, dictionaries have keys. These keys are labels that are used to access values within a dictionary."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a ><img src = \"https://ibm.box.com/shared/static/6tyznuwydogmtuv73o8l5g7xsb8o92h2.png\" width = 650, align = \"center\"></a>\n",
" <h4 align=center> A Comparison of a Dictionary to a list: Instead of the numerical indexes like a list, dictionaries have keys.\n",
" </h4> \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" An example of a Dictionary **Dict**:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'key1': 1,\n",
" 'key2': '2',\n",
" 'key3': [3, 3, 3],\n",
" 'key4': (4, 4, 4),\n",
" 'key5': 5,\n",
" (0, 1): 6}"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Dict={\"key1\":1,\"key2\":\"2\",\"key3\":[3,3,3],\"key4\":(4,4,4),('key5'):5,(0,1):6}\n",
"Dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" The keys can be strings:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Dict[\"key1\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Keys can also be any immutable object such as a tuple: "
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Dict[(0,1)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Each key is separated from its value by a colon \"**:**\". Commas separate the items, and the whole dictionary is enclosed in curly braces. An empty dictionary without any items is written with just two curly braces, like this \"**{}**\"."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'Thriller': '1982',\n",
" 'Back in Black': '1980',\n",
" 'The Dark Side of the Moon': '1973',\n",
" 'The Bodyguard': '1992',\n",
" 'Bat Out of Hell': '1977',\n",
" 'Their Greatest Hits (1971-1975)': '1976',\n",
" 'Saturday Night Fever': '1977',\n",
" 'Rumours': '1977'}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"release_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\"}\n",
"release_year_dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In summary, like a list, a dictionary holds a sequence of elements. Each element is represented by a key and its corresponding value. Dictionaries are created with two curly braces containing keys and values separated by a colon. For every key, there can only be a single value, however, multiple keys can hold the same value. Keys can only be strings, numbers, or tuples, but values can be any data type."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is helpful to visualize the dictionary as a table, as in figure 9. The first column represents the keys, the second column represents the values.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a ><img src = \"https://ibm.box.com/shared/static/i45fppou18c3t0fuf2ikks48tod7chbl.png\" width = 650, align = \"center\"></a>\n",
" <h4 align=center> Figure 9: Table representing a Dictionary \n",
"\n",
" </h4> \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You will need this dictionary for the next two questions :"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'The Bodyguard': '1992', 'Saturday Night Fever': '1977'}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"soundtrack_dic = { \"The Bodyguard\":\"1992\", \"Saturday Night Fever\":\"1977\"}\n",
"soundtrack_dic "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### In the dictionary \"soundtrack_dict\" what are the keys ?"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['The Bodyguard', 'Saturday Night Fever'])"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"soundtrack_dic.keys()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Double-click __here__ for the solution.\n",
"<!-- Your answer is below:\n",
"The Keys \"The Bodyguard\" and \"Saturday Night Fever\" \n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### In the dictionary \"soundtrack_dict\" what are the values ?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Double-click __here__ for the solution.\n",
"<!-- Your answer is below:\n",
"The values are \"1992\" and \"1977\"\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can retrieve the values based on the names:"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"'1982'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"release_year_dict['Thriller'] "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This corresponds to: \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a ><img src = \"https://ibm.box.com/shared/static/glbwz23cgjjxqi7rjxn7me5i16gan7h7.png\" width = 500, align = \"center\"></a>\n",
" <h4 align=center> \n",
" Table used to represent accessing the value for \"Thriller\" \n",
"\n",
" </h4> \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Similarly for The Bodyguard \n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"release_year_dict['The Bodyguard'] "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a ><img src = \"https://ibm.box.com/shared/static/6t7bu8jusckaskukwq1k0a3im5ltcpsn.png \" width = 500, align = \"center\"></a>\n",
" <h4 align=center> \n",
"Accessing the value for the \"The Bodyguard\"\n",
"\n",
" </h4> \n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let us retrieve the keys of the dictionary using the method **release_year_dict()**:"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['Thriller', 'Back in Black', 'The Dark Side of the Moon', 'The Bodyguard', 'Bat Out of Hell', 'Their Greatest Hits (1971-1975)', 'Saturday Night Fever', 'Rumours'])"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"release_year_dict.keys() "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" You can retrieve the values using the method **`values()`**:"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dict_values(['1982', '1980', '1973', '1992', '1977', '1976', '1977', '1977'])"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"release_year_dict.values() "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can add an entry:"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'Thriller': '1982',\n",
" 'Back in Black': '1980',\n",
" 'The Dark Side of the Moon': '1973',\n",
" 'The Bodyguard': '1992',\n",
" 'Bat Out of Hell': '1977',\n",
" 'Their Greatest Hits (1971-1975)': '1976',\n",
" 'Saturday Night Fever': '1977',\n",
" 'Rumours': '1977',\n",
" 'Graduation': '2007'}"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"release_year_dict['Graduation']='2007'\n",
"release_year_dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can delete an entry: "
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"{'Back in Black': '1980',\n",
" 'The Dark Side of the Moon': '1973',\n",
" 'The Bodyguard': '1992',\n",
" 'Bat Out of Hell': '1977',\n",
" 'Their Greatest Hits (1971-1975)': '1976',\n",
" 'Saturday Night Fever': '1977',\n",
" 'Rumours': '1977'}"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"del(release_year_dict['Thriller'])\n",
"del(release_year_dict['Graduation'])\n",
"release_year_dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" We can verify if an element is in the dictionary: "
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'The Bodyguard' in release_year_dict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### The Albums 'Back in Black', 'The Bodyguard' and 'Thriller' have the following music recording sales in millions 50, 50 and 65 respectively:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### a) Create a dictionary “album_sales_dict” where the keys are the album name and the sales in millions are the values. "
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"album_sales_dict ={'Back in Black':50, 'The Bodyguard':50 , 'Thriller':65}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Double-click __here__ for the solution.\n",
"<!-- Your answer is below:\n",
"album_sales_dict= { \"The Bodyguard\":50, \"Back in Black\":50,\"Thriller\":65}\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### b) Use the dictionary to find the total sales of \"Thriller\":"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"65"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"album_sales_dict['Thriller']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Double-click __here__ for the solution.\n",
"<!-- Your answer is below:\n",
"album_sales_dict[\"Thriller\"]\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### c) Find the names of the albums from the dictionary using the method \"keys\":"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dict_keys(['Back in Black', 'The Bodyguard', 'Thriller'])"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"album_sales_dict.keys()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Double-click __here__ for the solution.\n",
"<!-- Your answer is below:\n",
"album_sales_dict.keys()\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### d) Find the names of the recording sales​ from the dictionary using the method \"values\":"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"dict_values([50, 50, 65])"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"album_sales_dict.values()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"Double-click __here__ for the solution.\n",
"<!-- Your answer is below:\n",
"album_sales_dict.values()\n",
"-->"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <a href=\"http://cocl.us/NotebooksPython101bottom\"><img src = \"https://ibm.box.com/shared/static/irypdxea2q4th88zu1o1tsd06dya10go.png\" width = 750, align = \"center\"></a>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\n",
"\n",
"# About the Authors: \n",
"\n",
" [Joseph Santarcangelo]( https://www.linkedin.com/in/joseph-s-50398b136/) has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" <hr>\n",
"Copyright &copy; 2017 [cognitiveclass.ai](cognitiveclass.ai?utm_source=bducopyrightlink&utm_medium=dswb&utm_campaign=bdu). This notebook and its source code are released under the terms of the [MIT License](https://bigdatauniversity.com/mit-license/)."
]
}
],
"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.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment