Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,346 changes: 1,346 additions & 0 deletions .ipynb_checkpoints/2-checkpoint.ipynb

Large diffs are not rendered by default.

1,346 changes: 1,346 additions & 0 deletions 2.ipynb

Large diffs are not rendered by default.

481 changes: 481 additions & 0 deletions your-code/.ipynb_checkpoints/challenge-1-checkpoint.ipynb

Large diffs are not rendered by default.

1,346 changes: 1,346 additions & 0 deletions your-code/.ipynb_checkpoints/challenge-2-checkpoint.ipynb

Large diffs are not rendered by default.

392 changes: 392 additions & 0 deletions your-code/.ipynb_checkpoints/challenge-3-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,392 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Challenge 3: Dictionaries\n",
"\n",
"In this challenge you will practice how to manipulate Python dictionaries. Before starting on this challenge, you are encouraged to review W3School's [Python Dictionary Examples and Methods](https://www.w3schools.com/python/python_dictionaries.asp).\n",
"\n",
"First thing you will practice is how to sort the keys in a dictionary. Unlike the list object, Python dictionary does not have a built-in *sort* method. You'll need to use FOR loops to to sort dictionaries either by key or by value.\n",
"\n",
"The dictionary below is a summary of the word frequency of Ed Sheeran's song *Shape of You*. Each key is a word in the lyrics and the value is the number of times that word appears in the lyrics."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"word_freq = {'love': 25, 'conversation': 1, 'every': 6, \"we're\": 1, 'plate': 1, 'sour': 1, 'jukebox': 1, 'now': 11, 'taxi': 1, 'fast': 1, 'bag': 1, 'man': 1, 'push': 3, 'baby': 14, 'going': 1, 'you': 16, \"don't\": 2, 'one': 1, 'mind': 2, 'backseat': 1, 'friends': 1, 'then': 3, 'know': 2, 'take': 1, 'play': 1, 'okay': 1, 'so': 2, 'begin': 1, 'start': 2, 'over': 1, 'body': 17, 'boy': 2, 'just': 1, 'we': 7, 'are': 1, 'girl': 2, 'tell': 1, 'singing': 2, 'drinking': 1, 'put': 3, 'our': 1, 'where': 1, \"i'll\": 1, 'all': 1, \"isn't\": 1, 'make': 1, 'lover': 1, 'get': 1, 'radio': 1, 'give': 1, \"i'm\": 23, 'like': 10, 'can': 1, 'doing': 2, 'with': 22, 'club': 1, 'come': 37, 'it': 1, 'somebody': 2, 'handmade': 2, 'out': 1, 'new': 6, 'room': 3, 'chance': 1, 'follow': 6, 'in': 27, 'may': 2, 'brand': 6, 'that': 2, 'magnet': 3, 'up': 3, 'first': 1, 'and': 23, 'pull': 3, 'of': 6, 'table': 1, 'much': 2, 'last': 3, 'i': 6, 'thrifty': 1, 'grab': 2, 'was': 2, 'driver': 1, 'slow': 1, 'dance': 1, 'the': 18, 'say': 2, 'trust': 1, 'family': 1, 'week': 1, 'date': 1, 'me': 10, 'do': 3, 'waist': 2, 'smell': 3, 'day': 6, 'although': 3, 'your': 21, 'leave': 1, 'want': 2, \"let's\": 2, 'lead': 6, 'at': 1, 'hand': 1, 'how': 1, 'talk': 4, 'not': 2, 'eat': 1, 'falling': 3, 'about': 1, 'story': 1, 'sweet': 1, 'best': 1, 'crazy': 2, 'let': 1, 'too': 5, 'van': 1, 'shots': 1, 'go': 2, 'to': 2, 'a': 8, 'my': 33, 'is': 5, 'place': 1, 'find': 1, 'shape': 6, 'on': 40, 'kiss': 1, 'were': 3, 'night': 3, 'heart': 3, 'for': 3, 'discovering': 6, 'something': 6, 'be': 16, 'bedsheets': 3, 'fill': 2, 'hours': 2, 'stop': 1, 'bar': 1}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Sort the keys of `word_freq` ascendingly.\n",
"\n",
"Please create a new dictionary called `word_freq2` based on `word_freq` with the keys sorted ascedingly.\n",
"\n",
"There are several ways to achieve that goal but many of the ways are beyond what we have covered so far in the course. There is one way that we'll describe employing what you have learned. Please feel free to use this way or any other way you want.\n",
"\n",
"1. First extract the keys of `word_freq` and convert it to a list called `keys`.\n",
"\n",
"1. Sort the `keys` list.\n",
"\n",
"1. Create an empty dictionary `word_freq2`.\n",
"\n",
"1. Use a FOR loop to iterate each value in `keys`. For each key iterated, find the corresponding value in `word_freq` and insert the key-value pair to `word_freq2`.\n",
"\n",
"Print out `word_freq2` to examine its keys and values. Your output should be:\n",
"\n",
"```python\n",
"{'a': 8, 'about': 1, 'all': 1, 'although': 3, 'and': 23, 'are': 1, 'at': 1, 'baby': 14, 'backseat': 1, 'bag': 1, 'bar': 1, 'be': 16, 'bedsheets': 3, 'begin': 1, 'best': 1, 'body': 17, 'boy': 2, 'brand': 6, 'can': 1, 'chance': 1, 'club': 1, 'come': 37, 'conversation': 1, 'crazy': 2, 'dance': 1, 'date': 1, 'day': 6, 'discovering': 6, 'do': 3, 'doing': 2, \"don't\": 2, 'drinking': 1, 'driver': 1, 'eat': 1, 'every': 6, 'falling': 3, 'family': 1, 'fast': 1, 'fill': 2, 'find': 1, 'first': 1, 'follow': 6, 'for': 3, 'friends': 1, 'get': 1, 'girl': 2, 'give': 1, 'go': 2, 'going': 1, 'grab': 2, 'hand': 1, 'handmade': 2, 'heart': 3, 'hours': 2, 'how': 1, 'i': 6, \"i'll\": 1, \"i'm\": 23, 'in': 27, 'is': 5, \"isn't\": 1, 'it': 1, 'jukebox': 1, 'just': 1, 'kiss': 1, 'know': 2, 'last': 3, 'lead': 6, 'leave': 1, 'let': 1, \"let's\": 2, 'like': 10, 'love': 25, 'lover': 1, 'magnet': 3, 'make': 1, 'man': 1, 'may': 2, 'me': 10, 'mind': 2, 'much': 2, 'my': 33, 'new': 6, 'night': 3, 'not': 2, 'now': 11, 'of': 6, 'okay': 1, 'on': 40, 'one': 1, 'our': 1, 'out': 1, 'over': 1, 'place': 1, 'plate': 1, 'play': 1, 'pull': 3, 'push': 3, 'put': 3, 'radio': 1, 'room': 3, 'say': 2, 'shape': 6, 'shots': 1, 'singing': 2, 'slow': 1, 'smell': 3, 'so': 2, 'somebody': 2, 'something': 6, 'sour': 1, 'start': 2, 'stop': 1, 'story': 1, 'sweet': 1, 'table': 1, 'take': 1, 'talk': 4, 'taxi': 1, 'tell': 1, 'that': 2, 'the': 18, 'then': 3, 'thrifty': 1, 'to': 2, 'too': 5, 'trust': 1, 'up': 3, 'van': 1, 'waist': 2, 'want': 2, 'was': 2, 'we': 7, \"we're\": 1, 'week': 1, 'were': 3, 'where': 1, 'with': 22, 'you': 16, 'your': 21}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['a',\n",
" 'about',\n",
" 'all',\n",
" 'although',\n",
" 'and',\n",
" 'are',\n",
" 'at',\n",
" 'baby',\n",
" 'backseat',\n",
" 'bag',\n",
" 'bar',\n",
" 'be',\n",
" 'bedsheets',\n",
" 'begin',\n",
" 'best',\n",
" 'body',\n",
" 'boy',\n",
" 'brand',\n",
" 'can',\n",
" 'chance',\n",
" 'club',\n",
" 'come',\n",
" 'conversation',\n",
" 'crazy',\n",
" 'dance',\n",
" 'date',\n",
" 'day',\n",
" 'discovering',\n",
" 'do',\n",
" 'doing',\n",
" \"don't\",\n",
" 'drinking',\n",
" 'driver',\n",
" 'eat',\n",
" 'every',\n",
" 'falling',\n",
" 'family',\n",
" 'fast',\n",
" 'fill',\n",
" 'find',\n",
" 'first',\n",
" 'follow',\n",
" 'for',\n",
" 'friends',\n",
" 'get',\n",
" 'girl',\n",
" 'give',\n",
" 'go',\n",
" 'going',\n",
" 'grab',\n",
" 'hand',\n",
" 'handmade',\n",
" 'heart',\n",
" 'hours',\n",
" 'how',\n",
" 'i',\n",
" \"i'll\",\n",
" \"i'm\",\n",
" 'in',\n",
" 'is',\n",
" \"isn't\",\n",
" 'it',\n",
" 'jukebox',\n",
" 'just',\n",
" 'kiss',\n",
" 'know',\n",
" 'last',\n",
" 'lead',\n",
" 'leave',\n",
" 'let',\n",
" \"let's\",\n",
" 'like',\n",
" 'love',\n",
" 'lover',\n",
" 'magnet',\n",
" 'make',\n",
" 'man',\n",
" 'may',\n",
" 'me',\n",
" 'mind',\n",
" 'much',\n",
" 'my',\n",
" 'new',\n",
" 'night',\n",
" 'not',\n",
" 'now',\n",
" 'of',\n",
" 'okay',\n",
" 'on',\n",
" 'one',\n",
" 'our',\n",
" 'out',\n",
" 'over',\n",
" 'place',\n",
" 'plate',\n",
" 'play',\n",
" 'pull',\n",
" 'push',\n",
" 'put',\n",
" 'radio',\n",
" 'room',\n",
" 'say',\n",
" 'shape',\n",
" 'shots',\n",
" 'singing',\n",
" 'slow',\n",
" 'smell',\n",
" 'so',\n",
" 'somebody',\n",
" 'something',\n",
" 'sour',\n",
" 'start',\n",
" 'stop',\n",
" 'story',\n",
" 'sweet',\n",
" 'table',\n",
" 'take',\n",
" 'talk',\n",
" 'taxi',\n",
" 'tell',\n",
" 'that',\n",
" 'the',\n",
" 'then',\n",
" 'thrifty',\n",
" 'to',\n",
" 'too',\n",
" 'trust',\n",
" 'up',\n",
" 'van',\n",
" 'waist',\n",
" 'want',\n",
" 'was',\n",
" 'we',\n",
" \"we're\",\n",
" 'week',\n",
" 'were',\n",
" 'where',\n",
" 'with',\n",
" 'you',\n",
" 'your']"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here\n",
"keys = list(word_freq.keys())\n",
"keys.sort()\n",
"keys"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"word_freq2 = {}"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [],
"source": [
"def dict_iteration(key):\n",
" value = keys[key]\n",
" return print(keys[key],value)"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'a' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"Input \u001b[1;32mIn [36]\u001b[0m, in \u001b[0;36m<cell line: 1>\u001b[1;34m()\u001b[0m\n\u001b[1;32m----> 1\u001b[0m dict_iteration(\u001b[43ma\u001b[49m)\n",
"\u001b[1;31mNameError\u001b[0m: name 'a' is not defined"
]
}
],
"source": [
"dict_iteration(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Sort the values of `word_freq` ascendingly.\n",
"\n",
"Sorting the values of a dictionary is more tricky than sorting the keys because a dictionary's values are not unique. Therefore you cannot use the same way you sorted dict keys to sort dict values.\n",
"\n",
"The way to sort a dict by value is to utilize the `sorted` and `operator.itemgetter` functions. The following code snippet is provided to you to try. It will give you a list of tuples in which each tuple contains the key and value of a dict item. And the list is sorted based on the dict value ([reference](http://thomas-cokelaer.info/blog/2017/12/how-to-sort-a-dictionary-by-values-in-python/)\n",
").\n",
"\n",
"```python\n",
"import operator\n",
"sorted_tups = sorted(word_freq.items(), key=operator.itemgetter(1))\n",
"print(sorted_tups)\n",
"```\n",
"\n",
"Therefore, the steps to sort `word_freq` by value are:\n",
"\n",
"* Using `sorted` and `operator.itemgetter`, obtain a list of tuples of the dict key-value pairs which is sorted on the value.\n",
"\n",
"* Create an empty dictionary named `word_freq2`.\n",
"\n",
"* Iterate the list of tuples. Insert each key-value pair into `word_freq2` as an object.\n",
"\n",
"Print `word_freq2` to confirm your dictionary has its values sorted. Your output should be:\n",
"\n",
"```python\n",
"{'conversation': 1, \"we're\": 1, 'plate': 1, 'sour': 1, 'jukebox': 1, 'taxi': 1, 'fast': 1, 'bag': 1, 'man': 1, 'going': 1, 'one': 1, 'backseat': 1, 'friends': 1, 'take': 1, 'play': 1, 'okay': 1, 'begin': 1, 'over': 1, 'just': 1, 'are': 1, 'tell': 1, 'drinking': 1, 'our': 1, 'where': 1, \"i'll\": 1, 'all': 1, \"isn't\": 1, 'make': 1, 'lover': 1, 'get': 1, 'radio': 1, 'give': 1, 'can': 1, 'club': 1, 'it': 1, 'out': 1, 'chance': 1, 'first': 1, 'table': 1, 'thrifty': 1, 'driver': 1, 'slow': 1, 'dance': 1, 'trust': 1, 'family': 1, 'week': 1, 'date': 1, 'leave': 1, 'at': 1, 'hand': 1, 'how': 1, 'eat': 1, 'about': 1, 'story': 1, 'sweet': 1, 'best': 1, 'let': 1, 'van': 1, 'shots': 1, 'place': 1, 'find': 1, 'kiss': 1, 'stop': 1, 'bar': 1, \"don't\": 2, 'mind': 2, 'know': 2, 'so': 2, 'start': 2, 'boy': 2, 'girl': 2, 'singing': 2, 'doing': 2, 'somebody': 2, 'handmade': 2, 'may': 2, 'that': 2, 'much': 2, 'grab': 2, 'was': 2, 'say': 2, 'waist': 2, 'want': 2, \"let's\": 2, 'not': 2, 'crazy': 2, 'go': 2, 'to': 2, 'fill': 2, 'hours': 2, 'push': 3, 'then': 3, 'put': 3, 'room': 3, 'magnet': 3, 'up': 3, 'pull': 3, 'last': 3, 'do': 3, 'smell': 3, 'although': 3, 'falling': 3, 'were': 3, 'night': 3, 'heart': 3, 'for': 3, 'bedsheets': 3, 'talk': 4, 'too': 5, 'is': 5, 'every': 6, 'new': 6, 'follow': 6, 'brand': 6, 'of': 6, 'i': 6, 'day': 6, 'lead': 6, 'shape': 6, 'discovering': 6, 'something': 6, 'we': 7, 'a': 8, 'like': 10, 'me': 10, 'now': 11, 'baby': 14, 'you': 16, 'be': 16, 'body': 17, 'the': 18, 'your': 21, 'with': 22, \"i'm\": 23, 'and': 23, 'love': 25, 'in': 27, 'my': 33, 'come': 37, 'on': 40}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your code here\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Convert `word_freq` into Pandas dataframes\n",
"\n",
"In your future work, you may need to convert Python dictionaries to Pandas dataframes. So let's practice this by converting `word_freq`.\n",
"\n",
"**First, import the `pandas` library.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Then, use the `pd.DataFrame()` constructor to convert `word_freq` into a dataframe.**\n",
"\n",
"Here's a [reference](https://stackoverflow.com/questions/18837262/convert-python-dict-into-a-dataframe) to show you how to accomplish this. Also name the two columns of the dataframe as `word` and `freq`.\n",
"\n",
"Assign the converted value to a variable called `df`. The first few rows of `df` should look like this:\n",
"\n",
"![df](df.png)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your code here\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With the Pandas DataFrame, you can sort the values easily with the built-in method `sort_values` ([reference](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html)).\n",
"\n",
"#### Sort `df` ascendingly based on column `word`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your code here\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Sort `df` ascendingly based on column `freq`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Your code here\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading